Traversal of Circular Linked List in DSA Go - Time & Space Complexity
We want to understand how long it takes to visit every node in a circular linked list.
How does the time grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
func traverseCircularList(head *Node) {
if head == nil {
return
}
current := head
for {
fmt.Println(current.value)
current = current.next
if current == head {
break
}
}
}
This code visits each node in a circular linked list exactly once, starting from the head.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop visiting each node once.
- How many times: Exactly once per node, until it returns to the start.
As the list grows, the number of nodes to visit grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 visits |
| 100 | 100 visits |
| 1000 | 1000 visits |
Pattern observation: The operations grow linearly with the number of nodes.
Time Complexity: O(n)
This means the time to traverse grows directly with the number of nodes in the list.
[X] Wrong: "Because the list is circular, traversal might never end or take longer than linear time."
[OK] Correct: The loop stops once it returns to the starting node, so it visits each node only once, keeping time linear.
Understanding traversal time helps you explain how linked lists work and shows you can reason about loops that depend on structure, not just array indexes.
"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 list loops back = D [OK]
- Thinking circular list nodes have two pointers
- Confusing circular list with doubly linked list
- Assuming last node points to null
Solution
Step 1: Identify correct loop condition for circular list
Traversal must stop when current returns to head to avoid infinite loop.Step 2: Check code options
current := head for current != nil { fmt.Println(current.data) current = current.next if current == head { break } } uses a break when current equals head, correctly stopping traversal.Final Answer:
current := head for current != nil { fmt.Println(current.data) current = current.next if current == head { break } } -> Option CQuick Check:
Stop when current == head = A [OK]
- Using nil as stop condition in circular list
- Not breaking loop causing infinite traversal
- Incorrect loop condition skipping nodes
type Node struct {
data int
next *Node
}
func main() {
n1 := &Node{data: 10}
n2 := &Node{data: 20}
n3 := &Node{data: 30}
n1.next = n2
n2.next = n3
n3.next = n1
current := n1
for {
fmt.Print(current.data, " ")
current = current.next
if current == n1 {
break
}
}
}Solution
Step 1: Understand the circular linked list setup
Nodes 10 -> 20 -> 30 -> back to 10 form a circle.Step 2: Trace the loop output
Loop prints current.data then moves to next. Stops when current == n1 again, so prints 10, 20, 30 once.Final Answer:
10 20 30 -> Option BQuick Check:
Print nodes once before loop breaks = C [OK]
- Printing extra node after loop break
- Starting traversal from wrong node
- Confusing output order
current := head
for current != nil {
fmt.Println(current.data)
current = current.next
}Solution
Step 1: Analyze loop condition for circular list
Checking current != nil never becomes false in circular list, causing infinite loop.Step 2: Correct stopping condition
Loop should stop when current returns to head node to avoid infinite traversal.Final Answer:
The loop condition should check if current equals head to stop. -> Option DQuick Check:
Stop when current == head to avoid infinite loop [OK]
- Using nil as loop end in circular list
- Not breaking loop causing infinite print
- Misplacing print statement
Solution
Step 1: Understand traversal in circular linked list
Traversal must stop when current node equals head again to avoid infinite loop.Step 2: Summation logic
Start at head, add each node's data to sum, move to next node, stop when back at head.Final Answer:
Start from head, add node data, move to next, stop when current equals head again. -> Option AQuick Check:
Stop at head to sum all nodes once [OK]
- Stopping at nil causing infinite loop
- Using recursion without base case
- Traversing nodes multiple times
Ready to prove it?
Pass the test and this pattern moves unknown
