Web Analytics

ā­• Circular Linked List

Intermediate ~20 min read ⭐ Classic Problems

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

Output
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

  1. Structure: Last node points to head (no null!)
  2. Infinite traversal: Can go around forever
  3. Termination: Use current.next != head (not != null)
  4. Josephus problem: THE classic circular list problem
  5. Perfect for: Round-robin, circular buffers, cyclic processes
  6. 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!

Practice: Implement the Josephus problem. Try round-robin scheduling simulation!