Codectionary / Developer documentation / C#

LinkedList<T>

LinkedList<T> is a doubly-linked list implementation from System.Collections.Generic. Unlike List<T>, it stores elements as individual nodes linked to their neighbors, which makes inserting and removing elements at the beginning, end, or middle (given a reference to the node) very fast, at the cost of slower access by index, since reaching a specific position means walking the chain.

Syntax

LinkedList<Type> list = new LinkedList<Type>();

Examples

Basic LinkedList Usage

Adding elements to the front and back of a LinkedList<T>.

LinkedList<string> tasks = new LinkedList<string>();
tasks.AddLast("Write code");
tasks.AddLast("Test code");
tasks.AddFirst("Plan project");  // fast - O(1), unlike List<T>.Insert(0, ...)

foreach (string task in tasks)
{
    Console.WriteLine(task);
}

Working with Nodes

LinkedList<T> exposes its nodes directly, allowing efficient insertion relative to a known position.

LinkedList<int> numbers = new LinkedList<int>();
numbers.AddLast(1);
numbers.AddLast(3);

LinkedListNode<int> node = numbers.Find(1);
numbers.AddAfter(node!, 2);  // insert between 1 and 3

Console.WriteLine(string.Join(", ", numbers));  // 1, 2, 3

Removing Elements

Removing from either end, or a specific value, all in constant or near-constant time.

LinkedList<string> queue = new LinkedList<string>();
queue.AddLast("first");
queue.AddLast("second");
queue.AddLast("third");

queue.RemoveFirst();
Console.WriteLine(string.Join(", ", queue));  // second, third

queue.Remove("third");
Console.WriteLine(string.Join(", ", queue));  // second

Best practices

  • Choose LinkedList<T> over List<T> when your code frequently inserts or removes elements at the beginning, end, or a known middle position
  • Choose List<T> instead when you mostly need fast random access by index - LinkedList<T> access by index is slow since it must walk the chain
  • Use AddFirst()/AddLast()/AddBefore()/AddAfter() with a known node reference to get the real performance benefit LinkedList<T> offers
  • For simple stack or queue behavior specifically, prefer the dedicated Stack<T>/Queue<T> types over LinkedList<T> - they are simpler and just as efficient

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

List<T>
List<T> is a resizable, generic collection from System.Collections.Generic, and the most commonly used collection type in C#. Unlike a plain array, a List<T> automatically grows as elements are added, and it provides a rich set of methods for adding, removing, searching, and sorting. The <T> means a List can be strongly typed to hold any specific type, like List<string> or List<int>.
Dictionary<TKey, TValue>
Dictionary<TKey, TValue> stores data as key-value pairs, offering fast average-case lookup, insertion, and deletion by key, backed by a hash table. Keys must be unique - adding a value with an existing key throws an exception, while indexer assignment (dict[key] = value) overwrites it instead. Dictionary does not guarantee any particular iteration order.
Queue<T> & Stack<T>
Queue<T> is a first-in-first-out (FIFO) collection - items are added with Enqueue() and removed with Dequeue(), just like a real-world line. Stack<T> is last-in-first-out (LIFO) - items are added with Push() and removed with Pop(), like a stack of plates. Both are useful for specific processing orders where a general-purpose List<T> would require extra bookkeeping.
HashSet<T>
HashSet<T> is a collection that stores unique elements with no guaranteed ordering, backed by a hash table. Adding a duplicate element has no effect, since HashSet automatically enforces uniqueness. It provides very fast average-case performance for adding, removing, and checking membership, and offers built-in set operations like union, intersection, and difference.