The home where you level up

Save my spot

Circular Doubly Linked List

Term side of the Card.
Topic: Linked List
Unit title: Circular Doubly Linked List
Categories: Data structure
Explanation side of the Card

Explanation: The linked structure in which each node links to its previous and next nodes and the last node links back to the first. Enables bi-directional, circular traversal.

Example

Wrongjs
function append(head, node) {
  const tail = head.prev;
  tail.next = node;
  node.prev = tail;
}
Rightjs
function append(head, node) {
  const tail = head.prev;
  tail.next = node;
  node.prev = tail;
  node.next = head;
  head.prev = node;
}

The new tail must link back to head, and head.prev must point to it, or the circle breaks.

Common mistakes

  • Forgetting to update head.prev when appending, so backward traversal skips the new tail.
  • Infinite loops from checking for null instead of the start node.

Key points

  • After every insert and delete, head.prev is the tail and tail.next is the head.
  • A one-node list sets both next and prev to the node itself.
  • Traversal in either direction stops on returning to the start node.

Interview questions

Why use a circular doubly linked list instead of a doubly linked list?

You reach the tail from the head in O(1) through head.prev, and the ends need no null checks, which makes insert and delete simpler.

How do I get the last node of a circular doubly linked list?

Read head.prev. It always points to the tail, so no traversal is needed.

The home where you level up

Daily 3-minute lessons to understand your AI's code.

No spam — just launch news.
Unsubscribe anytime. · Privacy Policy

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy