Doubly and Circular Linked Lists

Lesson 10 of 20

Watch head and tail rewire a doubly list in O(1), then follow a ring that loops back onto its own head.

The big picture

Why two pointers

A singly list only knows how to move forward. Give every node a second pointer — prev — and the list can walk both ways, delete without hunting, and even close itself into a ring.

Two pointers per node

A doubly node carries data, next AND prev. prev is the superpower: the list walks backward as easily as forward.

O(1) at both ends

head and tail pointers mean insert or remove at either end touches a couple of pointers — nothing ever shifts.

Delete without a hunt

Given a node, its neighbors arrive free via prev and next. Singly lists must walk from head just to find the predecessor.

The circular twist

Point tail->next back at head and the list becomes a ring with no null — walks stop on p == head instead.

You already use one

std::list is a doubly linked list; LRU caches, undo history and repeat-mode playlists ride the same shape.

The price

One extra pointer per node costs memory, and there is still no random access — reaching item i means walking i links.