Chapter 5 · Linked Lists

Circular Linked Lists

In a singly linked list the last node points to null. In a circular linked list it points back to the head — creating an endless ring with no beginning and no end.

What is a circular linked list?

A circular linked list is a linked list whose last node points back to the first instead of to null, forming a loop. It's useful for things that cycle forever, such as round-robin scheduling, turn-based games and music playlists on repeat. When traversing, stop when you get back to the starting node, or you'll loop forever.

const a = { v: "A" }, b = { v: "B" }, c = { v: "C" };
a.next = b; b.next = c; c.next = a;              // circular

let node = a;
do { console.log(node.v); node = node.next; } while (node !== a);

The ring structure

AHEAD
→
Bnode2
→
Cnode3
→
Dnode4
→
HEAD ↑

The tail node's next pointer wraps back to the head. There is no null sentinel — the list never “ends.” Traversal must use a stopping condition other than “next is null.”

Live demo — linked list operations

Build a linked list to understand the node-and-pointer structure. In a circular variant the tail's next would point back to the head instead of null.

Singly Linked List

length: 4
head↓null10next*25next*7next*43next*
new / reversedtraversingfounddeleting

Traversal and insertion pseudocode

circular-ll.pseudo
1traverse(head): // stop at head again, not null
2 if head = null: return
3 current <- head
4 loop:
5 visit(current.value)
6 current <- current.next
7 if current = head: break // back to start
8 
9insertAfter(node, value):
10 new <- Node(value)
11 new.next <- node.next // new points to node's old next
12 node.next <- new // node now points to new
13 
14insertAtHead(head, value):
15 new <- Node(value)
16 new.next <- head // new points to old head
17 tail.next <- new // tail wraps to new head
18 head <- new

Floyd's cycle detection

A circular linked list looks identical to a singly linked list with a cycle (a bug). Floyd's algorithm can detect this using two pointers moving at different speeds.

floyd-cycle.pseudo
1detectCycle(head):
2 slow <- head
3 fast <- head
4 loop:
5 slow <- slow.next // moves 1 step
6 fast <- fast.next.next // moves 2 steps
7 if fast = null: return false // no cycle (linear list)
8 if slow = fast: return true // met — cycle exists
9 
10// Why it works:
11// fast gains 1 step per iteration on slow.
12// In a cycle of length k, they meet within k iterations.
13// Space: O(1) — only two pointers needed.
🔍Tortoise and the hare
The slow pointer is the tortoise (1 step), the fast pointer is the hare (2 steps). If there is a loop, the hare will eventually lap the tortoise and they will occupy the same node. If the hare reaches null, there is no loop. Time: O(n). Space: O(1).

Real-world applications

Round-robin scheduling

OS process schedulers cycle through all tasks in order, wrapping from last to first endlessly.

Media playlist loop

A music player's loop mode is literally a circular linked list — the last song's next is the first song.

Circular buffer (ring buffer)

A fixed-size FIFO buffer where producer and consumer positions wrap around — used in network drivers, audio streams.

Multiplayer games

Turn order in board games rotates through players endlessly. A circular list represents the turn queue perfectly.

Time complexity

insertAfter(node) — given direct reference to nodeO(1)

Constant time

insertAtHead — with tail pointer maintainedO(1)

Constant time

insertAtTail — with tail pointer maintainedO(1)

Constant time

search — must traverse up to full circleO(n)

Linear time

delete arbitrary node — must find predecessorO(n)

Linear time

🎯 Quick Check

You are traversing a circular linked list and forgot to add a stopping condition. What happens?

⚠️Always maintain a tail pointer
Without a separate tail reference, inserting at the tail requires O(n) traversal to find the last node. Store a tail pointer alongside head to get O(1) tail insertion. The tail's defining property: tail.next = head.

Circular Linked Lists: summary

Key points

  • Keeping a pointer to the tail gives O(1) access to both the tail and the head (tail.next).
  • Floyd's fast/slow pointer algorithm detects whether a list contains a cycle.

Frequently asked questions

›How do you detect a cycle in a linked list?

Move a slow pointer one step and a fast pointer two steps at a time. If they ever meet, there's a cycle; if fast reaches null, there isn't.