Traversal of Circular Linked List in DSA Python - Time & Space Complexity
We want to understand how long it takes to go through all items in a circular linked list.
How does the time grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, data):
self.data = data
self.next = None
def traverse_circular_list(head):
if not head:
return
current = head
while True:
print(current.data)
current = current.next
if current == head:
break
This code prints each element in a circular linked list once, stopping when it loops back to the start.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that visits each node once.
- How many times: Exactly once for each node in the list, until it returns to the start.
As the list grows, the number of steps grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: The steps increase one-for-one with the number of nodes.
Time Complexity: O(n)
This means the time to traverse grows linearly with the number of nodes in the list.
[X] Wrong: "Since the list loops, traversal might never end or take infinite time."
[OK] Correct: The code stops when it returns to the start node, so it visits each node only once and ends properly.
Understanding traversal time helps you explain how linked lists work and shows you can reason about loops that cycle back.
"What if the list was singly linked but not circular? How would the time complexity change?"
Practice
Solution
Step 1: Understand the structure of a circular linked list
A circular linked list connects the last node back to the first node, forming a circle.Step 2: Compare with a regular singly linked list
A regular singly linked list ends with a node pointing to null, not back to the first node.Final Answer:
In a circular linked list, the last node points back to the first node. -> Option AQuick Check:
Circular linked list last node points to first node [OK]
- Thinking circular lists have two pointers per node
- Confusing circular linked list with doubly linked list
- Assuming last node points to null
Solution
Step 1: Understand traversal stopping condition for circular list
Traversal must stop when current node comes back to head to avoid infinite loop.Step 2: Check code options for correct loop and break condition
current = head while True: print(current.data) current = current.next if current == head: break uses while True with break when current equals head, correctly handling circular traversal.Final Answer:
Use while True loop and break when current equals head. -> Option BQuick Check:
Stop traversal when current == head [OK]
- Using None as stopping condition in circular list
- Forgetting to break loop causing infinite traversal
- Using length which is not defined for linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None
head = Node(10)
second = Node(20)
third = Node(30)
head.next = second
second.next = third
third.next = head # circular link
current = head
count = 0
while True:
print(current.data, end=' ')
current = current.next
count += 1
if count == 4:
breakSolution
Step 1: Trace the nodes and their links
Nodes are 10 -> 20 -> 30 -> back to 10 forming a circle.Step 2: Follow the loop for 4 iterations
Prints: 10 (count=1), 20 (2), 30 (3), 10 (4) then breaks.Final Answer:
10 20 30 10 -> Option CQuick Check:
4 prints following circular links [OK]
- Stopping after 3 prints assuming no repetition
- Printing wrong node data due to pointer confusion
- Ignoring circular link causing infinite loop
current = head
while current != head:
print(current.data)
current = current.nextSolution
Step 1: Analyze the loop condition at start
current is initialized to head, so current != head is False initially.Step 2: Understand loop execution
Since condition is False, loop body never executes, so no data is printed.Final Answer:
The loop never starts because current == head initially. -> Option DQuick Check:
Loop condition false at start stops loop [OK]
- Assuming loop runs at least once
- Using != head as loop condition without special handling
- Not using do-while style logic for circular lists
Solution
Step 1: Traverse circular list starting at head
Start at head node with value 5 and move to next nodes collecting values.Step 2: Stop traversal when current node returns to head
Collect values 5, 15, 25, 35 and stop before repeating 5 again.Final Answer:
[5, 15, 25, 35] -> Option AQuick Check:
Collect all unique nodes once [OK]
- Including head node twice causing extra element
- Stopping too early missing last node
- Infinite loop by not checking for head again
Ready to prove it?
Pass the test and this pattern moves unknown
