Get the first node of the LinkedList in C#

The LinkedList in C# is a doubly linked list collection that provides efficient insertion and removal of elements. To get the first node of a LinkedList, you use the First property, which returns a LinkedListNode<T> object containing the value and navigation references.

Syntax

Following is the syntax to access the first node of a LinkedList −

LinkedListNode<T> firstNode = linkedList.First;
T firstValue = linkedList.First.Value;

Properties

  • First − Returns the first LinkedListNode<T> in the LinkedList, or null if the list is empty.

  • First.Value − Gets the actual value stored in the first node.

  • Last − Returns the last LinkedListNode<T> in the LinkedList.

LinkedList Structure First Node "A" Node 2 "B" Node 3 "C" Last Node "D" list.First.Value = "A"

Using First Property with String LinkedList

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      LinkedList<String> list = new LinkedList<String>();
      list.AddLast("A");
      list.AddLast("B");
      list.AddLast("C");
      list.AddLast("D");
      list.AddLast("E");
      list.AddLast("F");
      list.AddLast("G");
      list.AddLast("H");
      list.AddLast("I");
      list.AddLast("J");
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("First Node = " + list.First.Value);
      list.Clear();
      Console.WriteLine("Count of nodes (updated) = " + list.Count);
   }
}

The output of the above code is −

Count of nodes = 10
First Node = A
Count of nodes (updated) = 0

Using First and Last Properties with Integer LinkedList

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      LinkedList<int> list = new LinkedList<int>();
      list.AddLast(100);
      list.AddLast(200);
      list.AddLast(300);
      list.AddLast(400);
      list.AddLast(500);
      list.AddLast(600);
      list.AddLast(700);
      list.AddLast(800);
      list.AddLast(900);
      list.AddLast(1000);
      Console.WriteLine("Count of nodes = " + list.Count);
      Console.WriteLine("First Node = " + list.First.Value);
      Console.WriteLine("Last Node = " + list.Last.Value);
      list.Clear();
      Console.WriteLine("Count of nodes (updated) = " + list.Count);
   }
}

The output of the above code is −

Count of nodes = 10
First Node = 100
Last Node = 1000
Count of nodes (updated) = 0

Handling Empty LinkedList

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      LinkedList<string> list = new LinkedList<string>();
      
      if (list.First != null) {
         Console.WriteLine("First Node: " + list.First.Value);
      } else {
         Console.WriteLine("LinkedList is empty");
      }
      
      list.AddFirst("Hello");
      list.AddLast("World");
      
      Console.WriteLine("First Node: " + list.First.Value);
      Console.WriteLine("Last Node: " + list.Last.Value);
   }
}

The output of the above code is −

LinkedList is empty
First Node: Hello
Last Node: World

Conclusion

The First property of LinkedList in C# provides direct access to the first node in the collection. Always check for null when the LinkedList might be empty, as accessing First.Value on an empty list will throw a NullReferenceException.

Updated on: 2026-03-17T07:04:36+05:30

437 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements