Doubly and Circular Linked Lists

Lesson 10 of 20

Walk a doubly-linked list forwards and backwards, then ride a circular list whose last node loops back to the first.

The big picture

The big picture

Two upgrades to the singly list: a backward link that makes walks two-directional, and a loop-back edge that removes null from the picture entirely.

Two links per node

A doubly node stores prev AND next. It can hop left or right — a singly node can only ever move forward.

Both ends are O(1)

Keep head AND tail: addFirst, addLast, removeFirst, removeLast — all constant time, no walking required.

LinkedList in the JDK

java.util.LinkedList is a doubly-linked list, and it implements Deque — that is why offerFirst and pollLast are O(1) on it.

Circular = no null

The last node points back to the first. Walks stop by testing curr == start, never curr == null — and a do-while fits naturally.

Removal needs order

Unlinking a doubly node takes two rewire moves: prev.next = node.next FIRST, then next.prev = node.prev. Wrong order and the chain tears.

Sentinel nodes

A dummy header node removes the empty-list and head-update special cases — real implementations lean on it, and MCQs love asking why.