Data Structures · Visualization
Doubly Linked List: Introduction
Doubly Linked List
A doubly linked list is like a singly linked list, but each node holds two pointers instead of one: 'next', pointing to the following node, and 'prev', pointing to the previous node. Time: O(1) to access head/tail, O(n) for an arbitrary node. Space: O(n) (extra pointer per node compared to a singly linked list).
Big-O: Insert/delete at either end O(1); extra memory for prev pointers.
Doubly Linked List: Introduction
Doubly Linked ListA doubly linked list is like a singly linked list, but each node holds two pointers instead of one: 'next', pointing to the following node, and 'prev', pointing to the previous node. This lets you walk the list in both directions, and makes some operations, like deleting a known node, much faster because you no longer need to track down its predecessor separately.
How Doubly Linked List: Introduction Works
- Create a node with a value, a 'next' pointer, and a 'prev' pointer.
- Link nodes both ways: nodeA.next = nodeB and nodeB.prev = nodeA.
- Keep a head pointer (first node) and often a tail pointer (last node) for fast access at both ends.
- The first node's prev is null, and the last node's next is null.
Doubly Linked List: Introduction Worked Example
Input: Build a doubly linked list holding 10, 20, 30
- node(10).next = node(20); node(20).prev = node(10)
- node(20).next = node(30); node(30).prev = node(20)
- head = node(10), tail = node(30)
Output: null <- 10 <-> 20 <-> 30 -> null
Press ▶ Run to watch it step by step.
class Node:
def __init__(self, val):
self.val = val
self.prev = None
self.next = None
# example: intro on [10, 20, 30]Doubly Linked List: Introduction Time & Space Complexity
When to Use Doubly Linked List: Introduction
- Browser back/forward navigation history
- Implementing an efficient LRU cache (fast removal from the middle)
- Music players or playlists that support both 'next' and 'previous'
- The 'undo' and 'redo' stacks in editors
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.