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
LinkedList in C#
The LinkedList<T> class in C# is a doubly-linked list implementation found in the System.Collections.Generic namespace. Unlike arrays or lists, a linked list allows for efficient insertion and removal of elements at any position without the need to shift other elements.
Each element in a LinkedList<T> is stored in a node that contains the data and references to both the next and previous nodes. This structure enables fast insertion and deletion operations at the cost of direct indexed access.
Syntax
Following is the syntax for creating a LinkedList −
LinkedList<T> linkedList = new LinkedList<T>();
Adding elements to the beginning or end −
linkedList.AddFirst(item); // Add to beginning linkedList.AddLast(item); // Add to end
How LinkedList Works
Basic LinkedList Operations
Adding Elements
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
LinkedList<string> linkedList = new LinkedList<string>();
// Add elements
linkedList.AddLast("First");
linkedList.AddLast("Second");
linkedList.AddFirst("Zero"); // Add at beginning
linkedList.AddLast("Third");
Console.WriteLine("LinkedList contents:");
foreach(var element in linkedList) {
Console.WriteLine(element);
}
Console.WriteLine("Count: " + linkedList.Count);
}
}
The output of the above code is −
LinkedList contents: Zero First Second Third Count: 4
Removing Elements
Example
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
LinkedList<int> numbers = new LinkedList<int>();
// Add some numbers
numbers.AddLast(10);
numbers.AddLast(20);
numbers.AddLast(30);
numbers.AddLast(40);
Console.WriteLine("Original LinkedList:");
foreach(var num in numbers) {
Console.Write(num + " ");
}
// Remove operations
numbers.RemoveFirst(); // Remove first element
numbers.RemoveLast(); // Remove last element
numbers.Remove(20); // Remove specific value
Console.WriteLine("\nAfter removals:");
foreach(var num in numbers) {
Console.Write(num + " ");
}
}
}
The output of the above code is −
Original LinkedList: 10 20 30 40 After removals: 30
Working with LinkedListNode
Example
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
LinkedList<string> cities = new LinkedList<string>();
cities.AddLast("New York");
cities.AddLast("London");
cities.AddLast("Tokyo");
// Find a specific node
LinkedListNode<string> londonNode = cities.Find("London");
if(londonNode != null) {
// Insert before and after London
cities.AddBefore(londonNode, "Paris");
cities.AddAfter(londonNode, "Berlin");
}
Console.WriteLine("Cities in LinkedList:");
foreach(var city in cities) {
Console.WriteLine(city);
}
}
}
The output of the above code is −
Cities in LinkedList: New York Paris London Berlin Tokyo
LinkedList vs List Comparison
| LinkedList<T> | List<T> |
|---|---|
| Fast insertion/deletion at any position (O(1)) | Slow insertion/deletion in middle (O(n)) |
| No indexed access (must traverse from head/tail) | Direct indexed access using list[index] (O(1)) |
| Higher memory overhead due to node pointers | Lower memory overhead, contiguous storage |
| Better for frequent insertions/deletions | Better for frequent random access operations |
Conclusion
LinkedList<T> in C# provides efficient insertion and deletion operations at any position through its doubly-linked structure. Use LinkedList when you need frequent modifications to the collection, but consider List<T> when you require indexed access to elements.
