Linked list
A chain of nodes where each holds a value and a pointer to the next: O(1) inserts and removals at known positions, O(n) to find anything.
Description
A linked list stores each value in its own node, and each node points to the next one. The list itself only needs to remember the first node, the head, and often the last, the tail. Nodes can live anywhere in memory.
That makes some operations very cheap. Inserting or removing a node when you already have a reference to its neighbor takes O(1): just change a couple of pointers, no shifting. Adding at the head or tail is O(1) too.
The price is access. There's no index arithmetic: to reach the 500th item you follow 500 pointers. Finding a value is O(n), and scanning is slower than an array in practice, because nodes are scattered in memory and each step can miss the CPU cache.
A doubly linked list adds a pointer to the previous node, so you can walk backwards and remove a node in O(1) given only the node itself. That's what LRU caches, browser history and many schedulers use internally.
How it works
A singly linked list: each node points to the next
- node 0: 12, next 7
- node 1: 7, next 31
- node 2: 31, next 5
- node 3: 5, next null
A doubly linked list: next and previous pointers
- node 0: 12, next 7
- node 1: 7, next 31
- node 2: 31, next 5
- node 3: 5, next null
Complexity
| Operation | Average | Worst |
|---|---|---|
| Access the i-th item | O(n) | O(n) |
| Search for a value | O(n) | O(n) |
| Insert or remove at the head | O(1) | O(1) |
| Insert at the tail (with a tail pointer) | O(1) | O(1) |
| Insert or remove next to a known nodeRemoving the node itself needs a doubly linked list. | O(1) | O(1) |
Space: O(n), plus one or two pointers per node
Linked lists look attractive on paper, but in most languages a dynamic array is faster for typical workloads because of memory locality. Reach for a linked list when you hold node references and need O(1) splicing.
TypeScript implementation
A doubly linked list with head and tail sentinels, supporting O(1) push, pop and removal of a known node.
// A doubly linked list with sentinel nodes at both ends, so inserts and
// removals never need special cases for an empty list or the first node.
export class ListNode<T> {
value: T;
prev: ListNode<T> | null = null;
next: ListNode<T> | null = null;
constructor(value: T) {
this.value = value;
}
}
export class LinkedList<T> {
private head = new ListNode<T>(undefined as T); // sentinel before the first node
private tail = new ListNode<T>(undefined as T); // sentinel after the last node
size = 0;
constructor() {
this.head.next = this.tail;
this.tail.prev = this.head;
}
private insertBefore(node: ListNode<T>, value: T): ListNode<T> {
const n = new ListNode(value);
n.prev = node.prev;
n.next = node;
node.prev!.next = n;
node.prev = n;
this.size++;
return n;
}
pushFront(value: T): ListNode<T> {
return this.insertBefore(this.head.next!, value);
}
pushBack(value: T): ListNode<T> {
return this.insertBefore(this.tail, value);
}
/** O(1): unlink a node you already hold a reference to. */
remove(node: ListNode<T>): T {
node.prev!.next = node.next;
node.next!.prev = node.prev;
this.size--;
return node.value;
}
popBack(): T | undefined {
return this.size ? this.remove(this.tail.prev!) : undefined;
}
*[Symbol.iterator](): Iterator<T> {
for (let n = this.head.next; n && n !== this.tail; n = n.next) yield n.value;
}
}
export function demo(): string[] {
const list = new LinkedList<number>();
list.pushBack(7);
const node31 = list.pushBack(31);
list.pushBack(5);
list.pushFront(12);
const lines = [`list: ${[...list].join(" → ")} (size ${list.size})`];
list.remove(node31); // O(1) with the node reference
lines.push(`after removing 31: ${[...list].join(" → ")}`);
lines.push(`popBack: ${list.popBack()}, list: ${[...list].join(" → ")}`);
return lines;
}When to use it
Use it when
- You keep references to nodes and need to remove or move them in O(1): LRU caches, task queues, undo history.
- You splice lists together or split them without copying.
- As the building block of other structures: queues, deques, adjacency lists, hash-table chains.
Avoid it when
- You need random access by index or binary search: use a dynamic array.
- You mostly iterate in order: an array is faster because of cache locality.
- Memory is tight: each node carries one or two extra pointers.
Pitfalls
- Losing the rest of the list by overwriting
nextbefore saving it during inserts and reversals. - Forgetting the edge cases: empty list, single node, removing the head or tail. Sentinel nodes remove most of them.
- Cycles created by mistake, which turn traversals into infinite loops. Floyd's tortoise-and-hare algorithm detects them.
- Assuming O(1) insertion everywhere: it's O(1) only once you have the position; finding it is O(n).
Alternatives
Dynamic array
Choose it when you need index access, sorting, or fast iteration. This is the right default.
Deque
Choose it when you only add and remove at the ends.
Balanced search tree
Choose it when you need ordered data with O(log n) search and insert.