Doubly Linked List

Deterministic node pointer links and sequential address transitions

HEAD
12idx: 0
dnode-0
next
prev
45idx: 1
dnode-1
next
prev
8idx: 2
dnode-2
next
prev
TAIL
27idx: 3
dnode-3
next
prev
NULL
null
Step 1 of 1Type: idle

Initialized doubly linked list with 4 nodes

Speed: 1x

Overview & Concept

A Doubly Linked List is a linear collection of nodes where each node contains two pointers: "next" pointing to the successor and "prev" pointing to the predecessor. This bidirectional linkage enables efficient bidirectional traversal, O(1) removal of known nodes without preceding node search, and serves as the core foundation for LRU caches and deque structures.

How It Works

Bidirectional Pointers: Each node stores both next and prev references.

Head and Tail Tracking: Pointers to both ends allow instant O(1) push and pop at both boundaries.

O(1) Node Self-Deletion: Given a pointer to node X, removing X is done directly by X.prev.next = X.next and X.next.prev = X.prev.

Bidirectional Traversal: Can iterate forwards from head or backwards from tail.

Step-by-Step Execution

1. Node CreationAllocate node with payload, next = null, prev = null.
2. Link SetupSet newNode.next = current and newNode.prev = current.prev.
3. Neighbor Re-pointingUpdate current.prev.next = newNode and current.prev = newNode.
4. Boundary CheckAdjust head or tail pointers if inserting at ends.

Asymptotic Complexity Analysis

MetricBest CaseAverage CaseWorst CaseSpace Complexity
Complexity BoundO(1) push/pop endsO(n) index accessO(n) traversalO(n)
ExplanationOptimal input arrangement minimizing operations.Expected time for uniformly distributed randomized inputs.Adversarial degenerate conditions requiring full traversal.Auxiliary memory allocated beyond original input size.

Pseudocode & Invariants

Algorithm Logic
// Doubly Linked List Operations
operation deleteNode(node):
  if node.prev != null: node.prev.next = node.next
  else: head = node.next
  if node.next != null: node.next.prev = node.prev
  else: tail = node.prev
  free(node)
Algorithmic Invariants
  • Bidirectional Consistency: For every node X where X.next != null: X.next.prev == X.
  • Boundary Invariants: head.prev == null and tail.next == null.
Correctness Guarantee: Invariants hold true prior to loop entry, are preserved during every iteration, and guarantee correct post-condition upon termination.

Advantages & Disadvantages

Key Advantages

  • •Bidirectional traversal in both directions from head or tail.
  • •O(1) deletion of an arbitrary node given its direct reference.
  • •O(1) insertion before or after any referenced node.

Trade-offs & Limitations

  • •Increased memory overhead: 2 pointers (prev, next) per node.
  • •More complex pointer management: every insertion/deletion requires 4 pointer updates.

Real-World Applications

LRU (Least Recently Used) Cache

Combined with a Hash Map to achieve O(1) get and O(1) eviction of stale items.

Browser Tab & History Navigation

Back and forward page navigation history in web browsers.

Music Playlist Players

Seamless skip forward and skip previous track transitions.

Decision Guide: When to Use

Ideal Scenarios

  • ✓When bidirectional traversal is required.
  • ✓When implementing LRU caches, Deques, or complex buffer managers.

Consider Alternatives When

  • ✕When memory per node is tightly constrained and unidirectional traversal suffices.

Compare trade-offs and complexity across complementary tools in AlgoViz.