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
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: 4Traversal and insertion pseudocode
1traverse(head): // stop at head again, not null2 if head = null: return3 current <- head4 loop:5 visit(current.value)6 current <- current.next7 if current = head: break // back to start89insertAfter(node, value):10 new <- Node(value)11 new.next <- node.next // new points to node's old next12 node.next <- new // node now points to new1314insertAtHead(head, value):15 new <- Node(value)16 new.next <- head // new points to old head17 tail.next <- new // tail wraps to new head18 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.
1detectCycle(head):2 slow <- head3 fast <- head4 loop:5 slow <- slow.next // moves 1 step6 fast <- fast.next.next // moves 2 steps7 if fast = null: return false // no cycle (linear list)8 if slow = fast: return true // met — cycle exists910// 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.
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
Constant time
Constant time
Constant time
Linear time
Linear time
🎯 Quick Check
You are traversing a circular linked list and forgot to add a stopping condition. What happens?
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.