3.2.4 · D2Linear Data Structures

Visual walkthrough — Circular linked list — applications

1,981 words9 min readBack to topic

This is the companion to the parent topic. If a symbol appears here you have not met, we stop and draw it.


Step 0 — What is a "node" and what is an "arrow"?

Look at the picture. Each cream box is a node. The value sits on the left; the burnt-orange arrow leaving the box is its next.

Figure — Circular linked list — applications

The teal curling arrow in the figure is the whole story: last.next = head.


Step 1 — Draw the circle we are counting around

Figure — Circular linked list — applications

We will not lean on the formula — we will walk the arrows instead, and only use the formula to check ourselves at the end.


Step 2 — How do we even move without falling off? (loop-safe traversal)

Figure — Circular linked list — applications

The figure shows the finger p hopping arrow-to-arrow. Notice the green flags (visited) light up all five nodes before p lands back on home — proof the do-while covers everyone exactly once.


Step 3 — Why deleting a person is one line ()

Figure — Circular linked list — applications

The plum arrow is the old route through the victim; the burnt-orange arrow is the new shortcut prev.next → victim.next. The victim floats away, forgotten. Counting simply continues from prev.next.


Step 4 — Play the game: eliminate the 1st victim (person 2)

Circle now: . Counting resumes from person 3.

Figure — Circular linked list — applications

The greyed box is person , spliced out. The new arrow is highlighted.


Step 5 — Continue: eliminate person 4, then person 1

Figure — Circular linked list — applications

Two eliminations shown: person (top), then the wrap that catches person (the teal arc from back to ).


Step 6 — The final duel and the survivor

Figure — Circular linked list — applications

The lone node curling into itself — the same self-loop we insert as the first node of any CLL, now reached as the last survivor. The circle's life ends where it began: one box pointing at itself.


Step 7 — Edge & degenerate cases (never skip these)


The one-picture summary

Figure — Circular linked list — applications

The whole game on one canvas: the five-seat circle, the elimination order drawn as fading plum arcs, and the surviving node glowing at the centre with its self-loop.

Recall Feynman: tell it to a 12-year-old

Five friends sit in a ring. You point at friend 1 and count "one, two" — friend 2 is out, so they leave and the ring closes up behind them. You keep counting from the next friend, always going round and round; when you reach the last seat you just keep going into the first seat like it's a racetrack, not a line. Every time you count to two, that friend leaves and the two friends beside them hold hands directly, skipping the gap. Do that over and over — 2 leaves, then 4, then 1 (that's the loop-around!), then 5 — until only friend 3 is left, holding their own hand in a tiny ring of one. The computer does the exact same thing: the "hold hands directly" is one arrow getting redrawn, and "keep going round" is the last arrow curling back to the first. That curling arrow is the entire magic of a circular linked list.

Recall Quick self-check

Why do we resume counting from prev.next after a delete? ::: Because victim.next (which prev now points to) is the very next living person, so counting continues seamlessly with no gap. For , who is the survivor and why? ::: Person 3 — eliminations run 2, 4, 1, 5; the wrap from 5 back to 1 (handled free by the circular arrow) is what leaves 3 last. What does p.next == p signal? ::: A one-node circle — the terminating state; that node is the survivor.


Related: Circular linked list — applications · Josephus Problem · Round-Robin Scheduling · Ring Buffer · Singly Linked List · Doubly Linked List · Queue · Fibonacci Heap