Doubly and Circular Linked Lists
Lesson 10 of 20Walk a doubly-linked list forwards and backwards, then ride a circular list whose last node loops back to the first.
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.
Two-way street
One doubly list: insert at the head, walk to the tail — then walk all the way back via prev.
A doubly linked list — every node stores prev AND next. head sits on 14, tail on 85.
Safe removal order
Deleting a node from a doubly list is two rewires — but the ORDER of those moves decides whether the chain survives.
Walkthrough
A doubly list growing at the tail, then a circular list doing exactly one full lap.
1public class DoublyList {2static class Node {3int value;4Node prev, next;5Node(int v) { value = v; }6}7static Node head, tail;89static void addBack(int v) {10Node node = new Node(v);11node.prev = tail;12if (tail == null) head = node;13else tail.next = node;14tail = node;15}1617public static void main(String[] args) {18addBack(31);19addBack(7);20addBack(54);21System.out.println(tail.value);22System.out.println(tail.prev.value);23System.out.println(head.prev);24}25}
head = null and tail = null — an empty list at BOTH ends.
Riding the ring
Four seats in a circle — the token never falls off, and a mid-ring deletion closes the gap in O(1).
Four carousel seats in a RING: 23’s next loops back to 6. There is no null anywhere to fall off.
Predict the output
A do-while checks AFTER the body — trace seat by seat before you guess.
1public class Predict {2public static void main(String[] args) {3int cabins = 5;4int seat = 1;5do {6System.out.println("cabin " + seat);7seat += 2;8} while (seat <= cabins);9System.out.println("last seat tried: " + seat);10}11}
Hint: The body runs first unconditionally, then seat is compared to cabins. Watch what seat becomes on the pass where the condition finally fails.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
A doubly node stores prev and next — walks go forwards AND backwards.
With head AND tail kept, insert and remove at both ends are O(1) — java.util.LinkedList is exactly this.
Delete a doubly node by rewiring prev.next first, then next.prev; move head if the node IS the head.
Circular lists have no null — the last node points back to the first.
Circular walks stop by testing curr == start; a do-while guarantees the first lap happens.
Sentinel (dummy header) nodes erase the empty-list and head-update special cases.