ā Circular Linked List
What if a linked list had no end? In a Circular Linked List, the last node points back to the first - forming a complete circle! No null termination, infinite traversal. Perfect for round-robin scheduling, circular buffers, and the famous Josephus problem!
šÆ The Circular Power!
Circular Linked List features:
- ā No null termination - last points to first!
- ā Infinite traversal - can go around forever!
- ā Perfect for cycles - round-robin, circular buffers
- ā Josephus problem - the classic circular list problem!
- ā ļø Avoid infinite loops - must use proper termination!
Natural for cyclic processes!
The Structure
Last Points to First!
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Create circular structure
last.next = head # No null!
Visual representation:
10 ā 20 ā 30 ā 40
ā ā
āāāāāāāāāāāāāāāāāā
Forms complete circle!
Key difference:
- Singly: last.next =
null - Circular: last.next =
headā
See It in Action
Watch the circular structure work:
The Circular Nature
Infinite Traversal
# Can traverse forever!
current = head
for i in range(100): # Go around many times
print(current.data)
current = current.next # Never reaches null!
Termination condition:
# Singly list: while current != null
# Circular list: while current.next != head
# Traverse once around
current = head
while True:
print(current.data)
current = current.next
if current == head: # Back to start!
break
The Josephus Problem
The CLASSIC circular linked list problem! N people stand in a circle. Starting from a given position, count k people and eliminate the kth person. Repeat until one person remains.
The Problem
Example: 7 people (1-7), eliminate every 3rd person
Circle: 1 ā 2 ā 3 ā 4 ā 5 ā 6 ā 7 ā (back to 1)
Round 1: Count 1, 2, 3 ā Eliminate 3
Circle: 1 ā 2 ā 4 ā 5 ā 6 ā 7 ā (back to 1)
Round 2: Count 4, 5, 6 ā Eliminate 6
Circle: 1 ā 2 ā 4 ā 5 ā 7 ā (back to 1)
Round 3: Count 7, 1, 2 ā Eliminate 2
Circle: 1 ā 4 ā 5 ā 7 ā (back to 1)
Continue until one remains...
Solution:
def josephus(n, k):
# Create circular list of n people
head = Node(1)
current = head
for i in range(2, n + 1):
current.next = Node(i)
current = current.next
current.next = head # Make circular!
# Eliminate every kth person
current = head
while current.next != current:
# Count k-1 steps
for _ in range(k - 1):
current = current.next
# Eliminate next person
print(f"Eliminated: {current.next.data}")
current.next = current.next.next
return current.data # Survivor!
The Complete Code
Click Run to execute your code
Real-World Applications
Where Circular Lists Shine
1. Round-Robin CPU Scheduling
- Processes in circular queue
- Each gets time slice
- Move to next, repeat forever
2. Circular Buffers
- Audio/video streaming
- Fixed-size buffer
- Write wraps around to start
3. Multiplayer Games
- Turn-based games
- Players in circle
- Next player after last = first
4. Music Playlists
- Repeat mode
- After last song, go to first
- Infinite playback
5. Network Packet Routing
- Token ring networks
- Token passes in circle
- Each node gets turn
Comparison with Other Lists
| Feature | Singly | Doubly | Circular |
|---|---|---|---|
| Last node points to | null | null | head ā |
| Can loop forever | No | No | Yes ā |
| Traverse backward | No | Yes ā | No |
| Memory per node | 2 pointers ā | 3 pointers | 2 pointers ā |
| Perfect for | Stacks, queues | LRU cache | Round-robin ā |
Common Mistakes
ā Mistake #1: Infinite Loop!
# WRONG - infinite loop!
current = head
while current: # Never null in circular list!
print(current.data)
current = current.next
# RIGHT - check if back to head
current = head
while True:
print(current.data)
current = current.next
if current == head: # Back to start!
break
ā Mistake #2: Forgetting to Make Circular
# WRONG - not circular!
last.next = None # Still singly linked!
# RIGHT - point back to head
last.next = head # Now circular!
ā Mistake #3: Wrong Termination in Josephus
# WRONG - checks for null (never happens!)
while current.next != None:
# RIGHT - check if only one left
while current.next != current:
Interview Tips
š” How to Ace Circular List Questions
- ā Always avoid infinite loops: Use proper termination!
- ā Check current.next != head: Not != null!
- ā Know Josephus problem: Classic circular list question!
- ā Mention round-robin: Real-world application!
- ā Draw the circle: Visualize the structure!
- ā Floyd's works here too: Detect circular structure!
ā When to Use Circular Lists
- Round-robin scheduling: CPU, network, games
- Circular buffers: Audio, video, logging
- Josephus problem: Elimination games
- Cyclic processes: Anything that repeats
- No natural end: Continuous loops
Summary
Circular Linked List Mastery
- Structure: Last node points to head (no null!)
- Infinite traversal: Can go around forever
- Termination: Use current.next != head (not != null)
- Josephus problem: THE classic circular list problem
- Perfect for: Round-robin, circular buffers, cyclic processes
- Avoid: Infinite loops - always use proper termination!
Interview tip: Circular lists are natural for cyclic processes. Know the Josephus problem - it appears frequently! Always check current.next != head, not != null!
What's Next?
You've mastered circular linked lists! Next: Advanced Linked List Problems - putting it all together!
Enjoying these tutorials?