Data Structures · Visualization
Circular Linked List: Introduction
Circular Linked List
A linked list variant where the last node's next pointer points back to the head instead of null, forming a ring. Time: Insert at end/front: O(1) with a tail pointer, O(n) without; Search/Traverse: O(n). Space: O(n) for n nodes.
Big-O: Same as a singly linked list — O(1) insert/delete at the ends (with a tail pointer), O(n) search/delete by value.
Circular Linked List: Introduction
Circular Linked ListA linked list variant where the last node's next pointer points back to the head instead of null, forming a ring. Can be singly or doubly circular, and traversal never naturally terminates — you must track a stop condition (e.g. stop when you reach the starting node again).
How Circular Linked List: Introduction Works
- Create nodes the same way as a regular linked list, each holding data and a next pointer.
- Instead of setting the last node's next to null, point it back to the head node.
- Keep a reference to head (and optionally tail) to know where a traversal should stop.
- When traversing, start at head and stop as soon as current.next === head.
Circular Linked List: Introduction Worked Example
Input: Insert 10, 20, 30 into an empty circular linked list
- Insert 10: head = tail = Node(10); Node(10).next = Node(10) (points to itself).
- Insert 20: tail.next = Node(20); tail = Node(20); Node(20).next = head.
- Insert 30: tail.next = Node(30); tail = Node(30); Node(30).next = head.
- Traverse from head: 10 → 20 → 30 → back to 10 (stop).
Output: Ring structure: 10 → 20 → 30 → (back to 10)
Press ▶ Run to watch it step by step.
class Node:
def __init__(self, v):
self.val = v
self.next = None
def build_circular(values):
head = None
tail = None
for v in values: # link tail -> head
node = Node(v)
if head is None:
head = tail = node
else:
tail.next = node
tail = node
tail.next = head # closes the ring
return head
head = build_circular([1, 2, 3])
# example: view on [10, 20, 30]Circular Linked List: Introduction Time & Space Complexity
When to Use Circular Linked List: Introduction
- Round-robin CPU scheduling
- Multiplayer turn-based games (cycling through players)
- Circular buffers / streaming data
- Josephus problem simulations
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.