Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
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
trueif an element was removed,falseif 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.
