ZetCode

C# LinkedList

last modified October 5, 2026

This article explains the generic LinkedList<T> collection in C#. It is a doubly linked list: each node points to both its previous and next node.

The collection is in System.Collections.Generic. Unlike List<T>, a linked list does not provide index-based access. Instead, it exposes nodes that can be traversed and used for insertion or removal.

C# LinkedList example

Create an empty list and append values with AddLast. Enumeration visits the values from the first node to the last.

Program.cs
using System.Collections.Generic;

var stations = new LinkedList<string>();
stations.AddLast("Central");
stations.AddLast("Museum");
stations.AddLast("Harbor");

foreach (var station in stations)
{
    Console.WriteLine(station);
}
$ dotnet run
Central
Museum
Harbor

Adding nodes at either end

AddFirst inserts at the beginning and AddLast at the end. Both return the created LinkedListNode<T>, which can be retained for later operations.

Program.cs
var steps = new LinkedList<string>();
steps.AddLast("wash");
steps.AddLast("rinse");
steps.AddFirst("prepare");

Console.WriteLine($"First: {steps.First!.Value}");
Console.WriteLine($"Last: {steps.Last!.Value}");
$ dotnet run
First: prepare
Last: rinse

Inserting and removing by node

After locating a node, insertion before or after it and removal of that node take constant time. Finding a value first requires a traversal, so use a saved node reference if repeated edits are needed.

Program.cs
var route = new LinkedList<string>(new[] { "A", "C" });
var stopA = route.Find("A")!;
var stopC = route.Find("C")!;

route.AddAfter(stopA, "B");
route.AddBefore(stopC, "B2");
route.Remove(stopA);

foreach (var stop in route)
{
    Console.WriteLine(stop);
}
$ dotnet run
B
B2
C

A node can belong to only one linked list at a time. Use RemoveFirst and RemoveLast to remove an endpoint, or pass a node to Remove when removing a known item.

Traversing nodes

The First and Last properties return endpoint nodes (or null for an empty list). A node's Next and Previous properties allow explicit traversal in either direction.

Program.cs
var numbers = new LinkedList<int>(new[] { 2, 4, 6 });

for (var node = numbers.Last; node is not null; node = node.Previous)
{
    Console.WriteLine(node.Value);
}
$ dotnet run
6
4
2

LinkedList or List?

Use LinkedList<T> when you need frequent insertions or removals at known nodes and do not need indexing. Use List<T> for fast index access and compact storage. Searching a linked list by value is linear, and each linked-list node uses extra memory to store its links.

Source

LinkedList<T> Class - .NET API

This article introduced the doubly linked list collection in C#.

Author

Jan Bodnar writes programming tutorials and educational content.

List all C# tutorials.