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 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. 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

  1. Create a node with a value, a 'next' pointer, and a 'prev' pointer.
  2. Link nodes both ways: nodeA.next = nodeB and nodeB.prev = nodeA.
  3. Keep a head pointer (first node) and often a tail pointer (last node) for fast access at both ends.
  4. 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

  1. node(10).next = node(20); node(20).prev = node(10)
  2. node(20).next = node(30); node(30).prev = node(20)
  3. head = node(10), tail = node(30)

Output: null <- 10 <-> 20 <-> 30 -> null

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
Doubly LL: Introduction
Like a linked list, but every node keeps a pointer to both the previous and the next node, so you can walk it in either direction.
head
17
prev · next
30
prev · next
43
prev · next
56
prev · next
tail
69
prev · next
Active node next pointer prev pointer
Doubly LL: Introduction
Press ▶ Run to watch it step by step.
Python · Doubly Linked List in Code
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

TimeO(1) to access head/tail, O(n) for an arbitrary node
SpaceO(n) (extra pointer per node compared to a singly linked list)

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.