Removing first occurrence of specified value from LinkedList in C#

The LinkedList<T>.Remove(T) method in C# removes the first occurrence of the specified value from a LinkedList. This method searches the LinkedList from the beginning and removes only the first matching node it encounters, leaving any subsequent duplicates unchanged.

Syntax

Following is the syntax for removing the first occurrence of a specified value −

public bool Remove(T value)

Parameters

  • value: The value to remove from the LinkedList.

Return Value

Returns true if the element is successfully found and removed; otherwise, false. This method also returns false if the value was not found in the original LinkedList.

Remove() Operation - First Occurrence Only Before: A B C A E A First 'A' removed After: B C A E A Other occurrences of 'A' remain unchanged

Using Remove() Method

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("A");
      list.AddLast("E");
      list.AddLast("F");
      list.AddLast("A");
      list.AddLast("H");
      
      Console.WriteLine("Original LinkedList (Count: " + list.Count + "):");
      foreach(string item in list) {
         Console.Write(item + " ");
      }
      Console.WriteLine();
      
      bool removed = list.Remove("A");
      Console.WriteLine("Remove 'A' result: " + removed);
      
      Console.WriteLine("LinkedList after removal (Count: " + list.Count + "):");
      foreach(string item in list) {
         Console.Write(item + " ");
      }
      Console.WriteLine();
   }
}

The output of the above code is −

Original LinkedList (Count: 8):
A B C A E F A H 
Remove 'A' result: True
LinkedList after removal (Count: 7):
B C A E F A H 

Removing Non-Existent Value

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("Three");
      list.AddLast("Four");
      
      Console.WriteLine("Original LinkedList:");
      foreach(string item in list) {
         Console.Write(item + " ");
      }
      Console.WriteLine();
      
      bool removed1 = list.Remove("Three");
      Console.WriteLine("Remove first 'Three': " + removed1);
      
      bool removed2 = list.Remove("Five");
      Console.WriteLine("Remove 'Five' (not found): " + removed2);
      
      Console.WriteLine("Final LinkedList:");
      foreach(string item in list) {
         Console.Write(item + " ");
      }
      Console.WriteLine();
   }
}

The output of the above code is −

Original LinkedList:
One Two Three Three Four 
Remove first 'Three': True
Remove 'Five' (not found): False
Final LinkedList:
One Two Three Four 

Key Points

  • The Remove() method only removes the first occurrence of the specified value.

  • If the value appears multiple times, subsequent occurrences remain in the LinkedList.

  • The method returns true if an element was removed, false if the value was not found.

  • Time complexity is O(n) as it searches linearly from the beginning.

Conclusion

The Remove() method provides an efficient way to remove the first occurrence of a specific value from a LinkedList in C#. It returns a boolean indicating success and only affects the first matching element, making it ideal for selective removal operations in linked data structures.

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

355 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements