Doubly Linked List Explained Simply

Learn what a doubly linked list is, how it works, and when to use it — explained in simple terms with visuals, real-life analogies, and beginner-friendly JavaScript code. Perfect for developers who want to master data structures step-by-step.

Doubly Linked List Explained Simply

If you’ve understood singly linked lists, the next step is learning about doubly linked lists — they’re like supercharged chains that let you go both forward and backward.


🧠 What Is a Doubly Linked List?

A doubly linked list is a data structure where each node stores:

  • A value
  • A pointer to the next node
  • A pointer to the previous node

It’s like a two-way train — you can move to the next station or go back.


🧩 Visual Representation

null ← [10 | ⬅ ➡] ↔ [20 | ⬅ ➡] ↔ [30 | ⬅ ➡] → null
  • Each box is a node
  • points to the previous node
  • points to the next node
  • You can move in both directions

🪢 Real-Life Analogy

Think of metro train coaches.
Each coach has couplings in the front and back, so the train can go forwards or reverse — that's a doubly linked list!


✏️ Node Structure in Code (JavaScript Example)

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

🏗 Creating a Simple Doubly Linked List

const node1 = new Node(10);
const node2 = new Node(20);
const node3 = new Node(30);

// Linking forward
node1.next = node2;
node2.next = node3;

// Linking backward
node2.prev = node1;
node3.prev = node2;

🔄 How Do You Traverse a Doubly Linked List?

Forward:

let current = node1;
while (current !== null) {
  console.log(current.value);
  current = current.next;
}

Backward:

let current = node3;
while (current !== null) {
  console.log(current.value);
  current = current.prev;
}

⚙️ Common Operations

Operation Description
Insertion Add a node at the beginning, middle, or end
Deletion Remove a node by value or position
Forward Traverse Go from head to tail
Backward Traverse Go from tail to head
Search Find a node with a specific value

⏱ Time and Space Complexity

Operation Time Complexity Space Complexity
Insert at Head O(1) O(1)
Insert at Tail O(1) (if tail tracked) O(1)
Delete Node O(1) (if node is known) O(1)
Search O(n) O(1)
Traverse O(n) O(1)

🧪 Real-Life Example: 🎼 Playlist With Skip-Back Feature

1. The Idea

A music player where you can skip forward or backward between songs — that’s a doubly linked list!


2. Full Code (JavaScript)

/* music-player-dll.js */

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

class MusicPlayer {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  insertAtEnd(song) {
    const node = new Node(song);
    if (!this.head) {
      this.head = this.tail = node;
      return;
    }
    this.tail.next = node;
    node.prev = this.tail;
    this.tail = node;
  }

  insertAtBeginning(song) {
    const node = new Node(song);
    if (!this.head) {
      this.head = this.tail = node;
      return;
    }
    node.next = this.head;
    this.head.prev = node;
    this.head = node;
  }

  deleteByValue(song) {
    let current = this.head;
    while (current) {
      if (current.song === song) {
        if (current.prev) current.prev.next = current.next;
        else this.head = current.next;

        if (current.next) current.next.prev = current.prev;
        else this.tail = current.prev;
        return;
      }
      current = current.next;
    }
  }

  traverseForward() {
    let current = this.head;
    while (current) {
      console.log("▶️", current.song);
      current = current.next;
    }
  }

  traverseBackward() {
    let current = this.tail;
    while (current) {
      console.log("⏪", current.song);
      current = current.prev;
    }
  }
}

/* ----- Demo ----- */
const player = new MusicPlayer();
player.insertAtEnd("Song A");
player.insertAtEnd("Song B");
player.insertAtEnd("Song C");

console.log("▶️ Forward:");
player.traverseForward();

console.log("\n⏪ Backward:");
player.traverseBackward();

player.deleteByValue("Song B");
console.log("\nAfter deleting 'Song B':");
player.traverseForward();

3. Operation Summary

Operation Action Description
Add at Front Add before current head Update prev and next
Add at End Add after current tail Update tail's next and new node’s prev
Delete Remove by title Adjust both prev and next pointers
Forward Traverse Head to Tail Use next
Backward Traverse Tail to Head Use prev

4. Real Analogy

Think of a DVD playlist — you can go to the next scene or go back to the previous one. The links work in both directions.


✅ When to Use a Doubly Linked List?

  • You need to navigate in both directions
  • You want faster deletion without re-traversing
  • You need flexibility in insertion/removal

⚠️ Limitations

  • Uses more memory (extra prev pointer)
  • Slightly more complex to implement than singly linked lists

📌 Summary

  • A doubly linked list allows forward and backward movement.
  • Each node points to both its next and previous.
  • Useful when direction control and fast removal are important.

🧭 What’s Next?

Ready for more?
➡ Coming up: Circular Linked List Made Simple

Happy learning! 💡