Circular Linked List in Data Structure
A Circular Linked List is a variation of a linked list in which the last node points back to the first node instead of pointing to None, forming a circular chain.
Types
- Singly Circular Linked List: each node points to the next, and the last node points back to the head.
- Doubly Circular Linked List: each node has both next and previous pointers, and the list forms a loop in both directions.
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
new_node.next = self.head
return
current = self.head
while current.next != self.head:
current = current.next
current.next = new_node
new_node.next = self.head
def traverse(self):
if not self.head:
return []
values = [self.head.data]
current = self.head.next
while current != self.head:
values.append(current.data)
current = current.next
return values
Complexity
| Operation | Time |
|---|---|
| Insert at end | O(n) without tail pointer, O(1) with tail pointer |
| Traversal | O(n) |
Applications
- Round-robin CPU scheduling.
- Implementing circular buffers for streaming data.
- Multiplayer turn-based games cycling through players.
- Managing playlists that loop back to the first song.
The Tail Pointer Trick for Constant-Time Inserts
In a circular list, the last node already points to the first one. So if you keep a reference to the tail instead of the head, you get both ends for free: the tail is tail and the head is tail.next. This makes inserting at the front and at the end constant-time operations.
class CircularList:
def __init__(self):
self.tail = None
def insert_front(self, data):
node = Node(data)
if self.tail is None:
node.next = node # a single node points to itself
self.tail = node
else:
node.next = self.tail.next # new node points to old head
self.tail.next = node # tail now points to new head
def insert_end(self, data):
self.insert_front(data)
self.tail = self.tail.next # the new head becomes the tail
def delete(self, key):
if self.tail is None:
return False
prev, cur = self.tail, self.tail.next
while True:
if cur.data == key:
if cur is prev: # it was the only node
self.tail = None
else:
prev.next = cur.next
if cur is self.tail:
self.tail = prev
return True
prev, cur = cur, cur.next
if cur is self.tail.next: # back at the head: not found
return False
Notice how insert_end reuses insert_front: in a ring, adding before the head and then moving the tail one step forward places the new node at the end.
Stopping Conditions: Avoiding the Infinite Loop
A normal linked list ends at None, so while current: stops on its own. A circular list never reaches None, and that same loop runs forever. Always stop when you return to the node you started from, and handle the empty list before the loop begins. A do-while style loop (run once, then check) is the cleanest pattern, which is why the delete method above uses while True with a check at the end.
Worked Example: The Josephus Problem
This classic puzzle fits a circular list perfectly. People stand in a circle, and every k-th person is removed until one remains. Take 5 people numbered 1 to 5, removing every second person:
- Count 1, 2 from person 1: person 2 is removed.
- Count from person 3: person 4 is removed.
- Count from person 5: person 1 is removed.
- Count from person 3: person 5 is removed.
- Person 3 is the survivor.
With a circular list, each removal is just relinking one pointer, and the counting wraps around naturally without any index arithmetic.
Picking Between Linear and Circular Lists
| Need | Better choice |
|---|---|
| Repeatedly cycle through items in turn | Circular list |
| Quick access to both first and last node with one pointer | Circular list with a tail reference |
| Simple traversal that ends naturally | Linear singly linked list |
| Frequent movement backwards | Doubly circular list |
Common mistakes
- Forgetting that a one-node list points to itself, then setting its next to
None. - Deleting the tail node without moving the tail reference back.
- Printing the list with a loop that never checks for the starting node.
Checklist: handle empty and single-node cases first, track the tail, and always stop when you return to the start.