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
C# program to remove the first occurrence of a node in a Linked List
The LinkedList class in C# provides an efficient way to store and manipulate collections of data. When you need to remove the first occurrence of a specific node from a LinkedList, you can use the Remove() method, which searches for the specified value and removes its first occurrence.
Syntax
The syntax for removing the first occurrence of a node from a LinkedList is −
bool result = linkedList.Remove(value);
Parameters
-
value − The value to remove from the LinkedList. The method removes the first node that contains this value.
Return Value
The Remove()bool value −
-
true − If the node was found and removed successfully.
-
false − If the value was not found in the LinkedList.
Example
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string[] students = {"Katie", "Jennifer", "Amy", "Vera"};
LinkedList<string> list = new LinkedList<string>(students);
Console.WriteLine("Original LinkedList:");
foreach (var stu in list) {
Console.WriteLine(stu);
}
// removing the first occurrence of "Vera"
bool removed = list.Remove("Vera");
Console.WriteLine("\nRemoval successful: " + removed);
Console.WriteLine("\nLinkedList after removing first occurrence of 'Vera':");
foreach (var stu in list) {
Console.WriteLine(stu);
}
}
}
The output of the above code is −
Original LinkedList: Katie Jennifer Amy Vera Removal successful: True LinkedList after removing first occurrence of 'Vera': Katie Jennifer Amy
Removing From LinkedList with Duplicate Values
When a LinkedList contains duplicate values, the Remove() method removes only the first occurrence −
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string[] names = {"John", "Alice", "John", "Bob", "John"};
LinkedList<string> list = new LinkedList<string>(names);
Console.WriteLine("Original LinkedList:");
foreach (var name in list) {
Console.WriteLine(name);
}
// removing first occurrence of "John"
bool removed = list.Remove("John");
Console.WriteLine("\nFirst 'John' removed: " + removed);
Console.WriteLine("\nLinkedList after removing first occurrence of 'John':");
foreach (var name in list) {
Console.WriteLine(name);
}
}
}
The output of the above code is −
Original LinkedList: John Alice John Bob John First 'John' removed: True LinkedList after removing first occurrence of 'John': Alice John Bob John
Handling Non-Existent Values
When you try to remove a value that doesn't exist in the LinkedList, the Remove() method returns false −
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string[] students = {"Katie", "Jennifer", "Amy"};
LinkedList<string> list = new LinkedList<string>(students);
Console.WriteLine("Attempting to remove 'David' from the list:");
bool removed = list.Remove("David");
Console.WriteLine("Removal successful: " + removed);
Console.WriteLine("\nLinkedList remains unchanged:");
foreach (var stu in list) {
Console.WriteLine(stu);
}
}
}
The output of the above code is −
Attempting to remove 'David' from the list: Removal successful: False LinkedList remains unchanged: Katie Jennifer Amy
Conclusion
The Remove() method in C# LinkedList efficiently removes the first occurrence of a specified value and returns a boolean indicating success. This method is useful when you need to remove specific elements while preserving the order of remaining nodes in the linked list.
