Chapter 8 · Linked Structures

Doubly Linked Lists

Add a single extra pointer — pointing backwards — and you unlock O(1) deletion of any node you already have a reference to, bidirectional traversal, and the internals of every LRU cache ever built.

What is a doubly linked list?

In a doubly linked list each node points to both the next and the previous node. You can walk in both directions and remove a node in O(1) when you already have a reference to it, because you can reach its neighbours directly. It's the structure behind LRU caches, browser history and undo/redo.

class Node { constructor(v) { this.value = v; this.prev = null; this.next = null; } }

function remove(node) {                // O(1) given the node
  if (node.prev) node.prev.next = node.next;
  if (node.next) node.next.prev = node.prev;
}

Node anatomy

Each node now has three fields: value, next, and prev. The head's prev is null; the tail's next is null. Every other node knows both its neighbour.

prev: null

42

next: 0x200

→←

prev: 0x100

17

next: 0x350

→←

prev: 0x200

93

next: null

Live demo — try the operations

Insert at head or tail in O(1) when you have the tail reference. Delete head in O(1) — the same logic extends to any node once you hold its reference, which is the doubly-linked-list superpower.

Singly Linked List

length: 4
head↓null10next*25next*7next*43next*
new / reversedtraversingfounddeleting

The O(1) deletion superpower

In a singly linked list, deleting a node requires its predecessor. In a doubly linked list, any node already knows its predecessor via prev. No traversal needed.

doubly-linked-list.pseudo
1deleteNode(node): // O(1) — no traversal
2 if node.prev != null:
3 node.prev.next <- node.next
4 else:
5 head <- node.next // node was the head
6 if node.next != null:
7 node.next.prev <- node.prev
8 else:
9 tail <- node.prev // node was the tail
10 
11insertAfter(node, value): // O(1)
12 newNode <- Node(value)
13 newNode.prev <- node
14 newNode.next <- node.next
15 if node.next != null: node.next.prev <- newNode
16 node.next <- newNode

LRU Cache — the killer application

An LRU (Least Recently Used) cache evicts the longest-idle item when full. The classic O(1) implementation combines a doubly linked list with a hash map:

1. Hash map

Maps each key to its node in the linked list. O(1) lookup to find any node directly — no traversal.

2. Doubly linked list

Maintains access order. Most-recently-used node at head. Least-recently-used node at tail.

3. On cache hit

Look up node via hash map (O(1)), then move it to the head (O(1) — because we have the node reference). Total: O(1).

4. On cache miss + full

Remove the tail node (O(1)), delete its key from hash map (O(1)), insert new node at head (O(1)), add to hash map (O(1)). Total: O(1).

🔍Why both structures are needed
Hash map alone: O(1) lookup but no ordering. Doubly linked list alone: O(1) reorder but O(n) lookup. Together: O(1) everything. This combination pattern appears in many real systems — caches, history managers, message queues.

Time complexity

Insert at head or tail (with tail pointer)O(1)

Constant time

Delete any node (given reference)O(1)

Constant time

Insert after/before any node (given reference)O(1)

Constant time

Access by indexO(n)

Linear time

Search by valueO(n)

Linear time

🎯 Quick Check

You have a doubly linked list used as an LRU cache. A key is accessed. What operations happen?

🧠Singly vs Doubly in one line
Use singly if you only need head insert/delete and forward traversal. Use doubly if you need to delete arbitrary nodes you have references to, or traverse backwards. The extra prev pointer costs one pointer of memory per node.

Doubly Linked Lists: summary

Key points

  • Each node costs an extra pointer of memory.
  • Sentinel (dummy) head and tail nodes remove most edge cases for empty lists.
  • Combined with a hash map, it gives O(1) LRU caches.

Frequently asked questions

›When should I use a doubly linked list instead of a singly linked list?

When you need to delete arbitrary nodes quickly or traverse backwards, for example in an LRU cache or a browser history.