0
0
DSA Pythonprogramming

Traversal of Circular Linked List in DSA Python

Choose your learning style9 modes available
Mental Model
A circular linked list loops back to the start, so traversal must stop when we return to the first node to avoid infinite loops.
Analogy: Imagine walking around a circular track; you keep moving forward until you reach the starting point again, then you stop.
head -> 1 -> 2 -> 3 ->
          ↑         ↓
          ← ← ← ← ← ←
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> back to 1 (circular), traverse and print all nodes
Goal: Visit each node exactly once and stop after returning to the start
Step 1: Start at head node with value 1
[curr->1] -> 2 -> 3 -> back to 1
Why: We begin traversal from the first node
Step 2: Move curr to next node with value 2
1 -> [curr->2] -> 3 -> back to 1
Why: Visit the next node in the list
Step 3: Move curr to next node with value 3
1 -> 2 -> [curr->3] -> back to 1
Why: Continue traversal to next node
Step 4: Move curr to next node which is head (value 1), stop traversal
[curr->1] -> 2 -> 3 -> back to 1
Why: We reached the start again, so traversal ends to avoid infinite loop
Result:
1 -> 2 -> 3 -> back to 1 (traversal stops here)
Annotated Code
DSA Python
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
        curr = self.head
        while curr.next != self.head:
            curr = curr.next
        curr.next = new_node
        new_node.next = self.head

    def traverse(self):
        if not self.head:
            print("List is empty")
            return
        curr = self.head
        while True:
            print(curr.data, end=" -> ")
            curr = curr.next
            if curr == self.head:
                break
        print("back to head")

# Driver code
cll = CircularLinkedList()
cll.append(1)
cll.append(2)
cll.append(3)
cll.traverse()
while curr.next != self.head:
advance curr to last node before head to append new node
while True:
start infinite loop to traverse circular list
print(curr.data, end=" -> ")
print current node's data
curr = curr.next
advance curr to next node
if curr == self.head:
stop traversal when we return to head node
OutputSuccess
1 -> 2 -> 3 -> back to head
Complexity Analysis
Time: O(n) because we visit each of the n nodes exactly once during traversal
Space: O(1) because we use only a few pointers regardless of list size
vs Alternative: Compared to linear linked list traversal, circular traversal requires a check to stop at head to avoid infinite loops
Edge Cases
empty list
prints 'List is empty' and stops
DSA Python
if not self.head:
    print("List is empty")
    return
single node list
prints the single node once and stops after returning to head
DSA Python
if curr == self.head:
    break
When to Use This Pattern
When you see a linked list where the last node points back to the first, use circular linked list traversal by stopping when you reach the head again to avoid infinite loops.
Common Mistakes
Mistake: Not checking if current node is head again, causing infinite loop
Fix: Add a condition to break traversal when current node equals head
Mistake: Not handling empty list before traversal
Fix: Check if head is None and handle empty case before traversal
Summary
It visits each node in a circular linked list exactly once by stopping when it returns to the start.
Use it when you need to process or print all elements in a circular linked list safely.
The key insight is to stop traversal when you reach the head again to avoid looping forever.