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 the specified node from the LinkedList in C#?
The LinkedList<T> class in C# provides several methods to remove nodes from the linked list. You can remove nodes by value using Remove() method or remove specific node references using Remove(LinkedListNode<T>).
Syntax
Following are the different syntaxes for removing nodes from a LinkedList −
// Remove by value (removes first occurrence) bool Remove(T value) // Remove specific node void Remove(LinkedListNode<T> node) // Remove first node void RemoveFirst() // Remove last node void RemoveLast()
Return Value
The Remove(T value) method returns true if the element is successfully removed; otherwise, false. Other removal methods are void and do not return values.
Using Remove() Method with Values
The Remove() method removes the first occurrence of the specified value from the LinkedList −
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(300);
list.AddLast(500);
Console.WriteLine("LinkedList elements...");
foreach(int i in list) {
Console.WriteLine(i);
}
// Remove first occurrence of 500
bool removed = list.Remove(500);
Console.WriteLine("Removed 500: " + removed);
Console.WriteLine("LinkedList elements...UPDATED");
foreach(int i in list) {
Console.WriteLine(i);
}
}
}
The output of the above code is −
LinkedList elements... 100 200 300 400 500 300 500 Removed 500: True LinkedList elements...UPDATED 100 200 300 400 300 500
Using Remove() with String Values
The same approach works with string LinkedLists −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList<string> list = new LinkedList<string>();
list.AddLast("Mark");
list.AddLast("David");
list.AddLast("Harry");
list.AddLast("John");
list.AddLast("Kevin");
Console.WriteLine("LinkedList elements...");
foreach(string str in list) {
Console.WriteLine(str);
}
// Remove Harry from the list
bool removed = list.Remove("Harry");
Console.WriteLine("Removed Harry: " + removed);
Console.WriteLine("LinkedList elements...UPDATED");
foreach(string str in list) {
Console.WriteLine(str);
}
}
}
The output of the above code is −
LinkedList elements... Mark David Harry John Kevin Removed Harry: True LinkedList elements...UPDATED Mark David John Kevin
Removing Specific Node References
You can also remove specific node references found using Find() or FindLast() methods −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
LinkedList<int> list = new LinkedList<int>();
list.AddLast(10);
list.AddLast(20);
list.AddLast(30);
list.AddLast(20);
list.AddLast(40);
Console.WriteLine("Original LinkedList:");
foreach(int i in list) {
Console.WriteLine(i);
}
// Find and remove the last occurrence of 20
LinkedListNode<int> node = list.FindLast(20);
if(node != null) {
list.Remove(node);
Console.WriteLine("Removed last occurrence of 20");
}
Console.WriteLine("Updated LinkedList:");
foreach(int i in list) {
Console.WriteLine(i);
}
}
}
The output of the above code is −
Original LinkedList: 10 20 30 20 40 Removed last occurrence of 20 Updated LinkedList: 10 20 30 40
Comparison of Removal Methods
| Method | Description | Return Type |
|---|---|---|
| Remove(T value) | Removes first occurrence of the specified value | bool |
| Remove(LinkedListNode<T>) | Removes the specified node | void |
| RemoveFirst() | Removes the first node | void |
| RemoveLast() | Removes the last node | void |
Conclusion
The LinkedList<T> class provides flexible methods for removing nodes. Use Remove(T value) to remove by value (first occurrence only), or use Remove(LinkedListNode<T>) with Find() or FindLast() to remove specific node occurrences. The Remove(T value) method returns a boolean indicating success or failure.
