Removing the node at the start of the LinkedList in C#

The LinkedList<T> class in C# provides the RemoveFirst() method to remove the node at the start of the linked list. This method removes the first node and automatically updates the list's internal structure and count.

Syntax

Following is the syntax for removing the first node from a LinkedList −

linkedList.RemoveFirst();

The method throws an InvalidOperationException if the LinkedList is empty.

RemoveFirst() Operation One Two Three Four After RemoveFirst() Two Three Four

Using RemoveFirst() Method

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      LinkedList<string> list = new LinkedList<string>();
      list.AddLast("One");
      list.AddLast("Two");
      list.AddLast("Three");
      list.AddLast("Four");
      
      Console.WriteLine("Original LinkedList:");
      foreach (string item in list) {
         Console.WriteLine(item);
      }
      Console.WriteLine("Count: " + list.Count);
      
      list.RemoveFirst();
      
      Console.WriteLine("\nAfter RemoveFirst():");
      foreach (string item in list) {
         Console.WriteLine(item);
      }
      Console.WriteLine("Count: " + list.Count);
   }
}

The output of the above code is −

Original LinkedList:
One
Two
Three
Four
Count: 4

After RemoveFirst():
Two
Three
Four
Count: 3

Multiple RemoveFirst() Operations

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      LinkedList<int> numbers = new LinkedList<int>();
      numbers.AddLast(10);
      numbers.AddLast(20);
      numbers.AddLast(30);
      numbers.AddLast(40);
      numbers.AddLast(50);
      
      Console.WriteLine("Original list: " + string.Join(", ", numbers));
      
      // Remove first node twice
      numbers.RemoveFirst();
      Console.WriteLine("After first RemoveFirst(): " + string.Join(", ", numbers));
      
      numbers.RemoveFirst();
      Console.WriteLine("After second RemoveFirst(): " + string.Join(", ", numbers));
      
      Console.WriteLine("Final count: " + numbers.Count);
   }
}

The output of the above code is −

Original list: 10, 20, 30, 40, 50
After first RemoveFirst(): 20, 30, 40, 50
After second RemoveFirst(): 30, 40, 50
Final count: 3

Handling Empty LinkedList

Example

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      LinkedList<string> list = new LinkedList<string>();
      list.AddLast("Only");
      
      Console.WriteLine("List before removal: " + string.Join(", ", list));
      Console.WriteLine("Count: " + list.Count);
      
      list.RemoveFirst();
      Console.WriteLine("List after removal: " + (list.Count == 0 ? "Empty" : string.Join(", ", list)));
      Console.WriteLine("Count: " + list.Count);
      
      // Safe removal check
      if (list.Count > 0) {
         list.RemoveFirst();
      } else {
         Console.WriteLine("Cannot remove from empty list");
      }
   }
}

The output of the above code is −

List before removal: Only
Count: 1
List after removal: Empty
Count: 0
Cannot remove from empty list

Conclusion

The RemoveFirst() method efficiently removes the first node from a LinkedList in C# and automatically updates the count. Always check if the list is empty before calling this method to avoid exceptions, or use safe removal patterns in your applications.

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

148 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements