The idea: the last node points back to the first

In a plain singly linked list the last node's next is null. In a circular linked list it points back to the first node instead. There is no end: following next from any node goes round the whole ring and comes back.

Left: a ring of five nodes 1, 2, 3, 4, 5 where 5 points back to 1; the only pointer the list keeps is tail, on node 5, and tail.next is the head 1. Right: inserting node 6 into the gap between 5 and 1 with two writes, 6.next = tail.next and tail.next = 6; at the front that is all, at the back tail = 6 is a third write.
One pointer is enough: tail gives the last node, tail.next gives the first, and both inserts use the same gap.

So the list only needs to keep tail. The head is always tail.next. The stats line at the top of the canvas shows both. Keeping head instead would be worse: to reach the last node from the head you would walk the whole ring.

Insert at either end with two or three writes

In a ring the front and the back are the same place: the gap between the tail and the head. A new node goes there with two pointer writes:

new.next  = tail.next    # new points to the head
tail.next = new          # the tail points to new
# front: done, new is the head now (it is tail.next)
# back:  tail = new      # one more write

Neither insert walks the list, so both are O(1) (Demo: insert at the front and at the back: node 6 takes 2 writes, node 7 takes 3, and the ring reads 6 → 1 → 2 → 3 → 4 → 5 → 7). An empty ring is the one special case: the first node points to itself and becomes the tail.

Delete needs the node before

A singly linked node cannot reach the node before it. So to unlink cur the code walks two pointers together, prev one step behind cur, and then writes

prev.next = cur.next
if cur == tail: tail = prev
free(cur)

Starting with prev = tail and cur = tail.next keeps prev correct from the first step, even when the head itself is deleted. The tail check matters: without it, tail would point at freed memory (Demo: the tail is unlinked, tail = prev: with n = 4 and k = 4 the first one out is 4, the tail, so tail moves to 3).

The Josephus problem on the ring

n people stand in a circle. Starting at the first, count 1, 2, …, k. The k-th person is out. Counting starts again at the next person. Who is the last one left? This is the Josephus problem, and a circular list is its natural model:

  1. Count 1 at cur, then hop k − 1 times: prev = cur; cur = cur.next.
  2. Unlink cur: prev.next = cur.next (and move tail if needed).
  3. cur = prev.next and repeat until cur.next == cur: one node pointing to itself.

With n = 7 and k = 3 the people leave in the order 3, 6, 2, 7, 5, 1, and 4 survives after 12 hops and 8 pointer writes (6 unlinks plus 2 tail moves) (Demo: n = 7, k = 3, ring and recurrence). With k = 1 there are no hops: prev stays the tail and the head is unlinked every round, so the last person, 6 of 6, survives (Demo: k = 1, the head goes every time). When k is bigger than the ring the count simply goes round more than once: n = 4, k = 6 takes 15 hops and leaves 3 (Demo: k bigger than the ring).

n, kOut, in orderSurvivorHops
7, 33 6 2 7 5 1412
10, 22 4 6 8 10 3 7 1 959
10, 33 6 9 2 7 1 8 5 10418
6, 11 2 3 4 560
4, 62 1 4315

The simulation does n − 1 rounds of k − 1 hops, so it costs O(n·k) time. Counting only (k − 1) mod m hops when m people are left skips the full laps, but the ring still has to be built and walked.

The recurrence J(n, k)

Number the positions from 0 at the head where counting starts. Let J(n, k) be the survivor's position. After the first person goes (position (k − 1) mod n), counting restarts at the next one, and the n − 1 people left form the same problem again, only numbered from a new start that is k places further on. So the answer for n − 1 people, shifted by k, is the answer for n:

J(1, k) = 0
J(n, k) = (J(n − 1, k) + k) mod n
Top row: positions 0 to 6 for n = 7, k = 3; position 2 is crossed out. Bottom row: the six people left, numbered again from 0 starting at old position 3: new 0, 1, 2, 3, 4, 5 sit at old 3, 4, 5, 6, 0, 1. The survivor of 6 people is at new position J(6) = 0, which is old position (0 + 3) mod 7 = 3, so J(7) = 3.
After the first person is out, the rest is a smaller ring that starts k places on: old = (new + k) mod n.

The Recurrence button fills that table row by row, with no list at all: n − 1 additions and mods, O(n) time and O(1) memory. For n = 7, k = 3 the rows are 0, 1, 1, 0, 3, 0, 3, so J(7, 3) = 3, position 4 from the head, which is person 4, the same as the ring. Demo: the table predicts, the ring confirms fills the table for n = 10, k = 3 first (J = 3, person 4) and then runs the ring to check it.

Every second person: 2L + 1

For k = 2 there is a closed form. Write n = 2m + L with 0 ≤ L < 2m. The survivor is at position 2L + 1 (counting from 1). For n = 10 = 8 + 2 that is 2·2 + 1 = 5, and the ring agrees: out go 2, 4, 6, 8, 10, 3, 7, 1, 9 (Demo: n = 10, k = 2 and the 2L + 1 rule). The reason: when n is a power of two, each lap removes exactly half and the count comes back to the first person, so person 1 survives. With L extra people, after L people are out the ring is a power of two and the next one to count is person 2L + 1.

n12345678910
survivor, k = 21131357135

Where circular lists are used

  • Round-robin scheduling: the run queue is walked in a circle, and the next task is always cur.next (see CPU scheduling).
  • The Linux kernel's struct list_head is a circular doubly linked list with a dummy head, so there is no null check at either end.
  • A linked-list queue can be one circular list with only a tail pointer: enqueue is the back insert above, dequeue unlinks tail.next.
  • A fixed-size ring buffer usually lives in an array instead, with wrap-around indices (circular array queue); a deque works at both ends the same way.

What the page leaves out

Circular doubly linked lists (they unlink a node without prev), a dummy head node, counting backwards or with a changing k, the faster O(k log n) recurrence for large n, the history of the puzzle, and real memory addresses: the canvas places nodes evenly on a circle only to keep the arrows short. The drawing holds at most 10 nodes and k up to 7.

On the mind map

This page on the Basics mind map · 13 pages in 5 branches · open it to see the whole map and test yourself in recall mode.