# Circular Doubly Linked List

> The Circular Doubly Linked List is a data structure that links each node to both its previous and next nodes, with the last node connecting back to the first.

## What is a circular doubly linked list?

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.

- Topic: Linked List
- Type: data structure

## Example

Wrong:

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

Right:

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

## Related cards

- [Doubly Linked List](https://wonderium.dev/algorithms/doubly-linked-list.md)
- [Circular Linked List](https://wonderium.dev/algorithms/circular-linked-list.md)
- [Linked List](https://wonderium.dev/algorithms/linked-list.md)
- [Node](https://wonderium.dev/algorithms/node.md)
- [O(1)](https://wonderium.dev/algorithms/o-1.md)

---

- Level: [Algorithms](https://wonderium.dev/algorithms.md)
- HTML version: https://wonderium.dev/algorithms/circular-doubly-linked-list
- Last updated: 2026-11-25
