Traversal of Circular Linked List in DSA Java - 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.
Node current = head;
do {
System.out.print(current.data + " -> ");
current = current.next;
} while (current != head);
System.out.print("head");
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: Visiting each node once to print its data.
- How many times: Exactly once per node, until we return to the start.
As the number of nodes grows, the time to visit all nodes grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The time grows 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: "Traversal of a circular linked list takes infinite time because it loops forever."
[OK] Correct: The code stops when it returns to the starting node, so it visits each node only once and ends.
Understanding traversal time helps you explain how linked lists work and shows you can analyze simple loops clearly.
"What if the list was singly linked but not circular? How would the time complexity change?"
Practice
Solution
Step 1: Understand traversal stopping condition for singly linked list
Traversal stops when the next pointer is null, indicating the end of the list.Step 2: Understand traversal stopping condition for circular linked list
Traversal stops when the traversal reaches the starting node again, since the list loops back.Final Answer:
In a circular linked list, traversal stops when the starting node is reached again. -> Option DQuick Check:
Stopping condition differs: null vs start node [OK]
- Stopping traversal at null in circular list
- Confusing stopping conditions between list types
- Assuming circular list ends with null pointer
Solution
Step 1: Check initialization and loop condition
Node current = head; do { System.out.println(current.data); current = current.next; } while(current != head); initializes current to head and uses a do-while loop to print and move until current equals head again, which is correct for circular lists.Step 2: Analyze other options
Node current = head; while(current != null) { System.out.println(current.data); current = current.next; } uses while(current != null), which is incorrect for circular lists as next is never null. Node current = head.next; while(current != head) { System.out.println(current.data); current = current.next; } starts from head.next and misses printing head data. Node current = null; do { System.out.println(current.data); current = current.next; } while(current != head); initializes current to null, causing NullPointerException.Final Answer:
Node current = head; do { System.out.println(current.data); current = current.next; } while(current != head); -> Option AQuick Check:
Use do-while with current != head [OK]
- Using while(current != null) for circular lists
- Starting traversal from head.next and missing head node
- Initializing current to null causing errors
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = head;
Node current = head;
do {
System.out.print(current.data + " ");
current = current.next;
} while(current != head);Solution
Step 1: Understand the circular linked list structure
The list nodes are 10 -> 20 -> 30, and 30 points back to 10, making it circular.Step 2: Trace the do-while loop output
Starting at head (10), print 10, move to 20, print 20, move to 30, print 30, move to 10 again. Loop stops when current == head, so 10 is not printed again.Final Answer:
10 20 30 -> Option BQuick Check:
Print nodes once until back to start [OK]
- Printing the start node twice
- Missing the first node in output
- Printing extra nodes beyond the loop
Node current = head;
while(current != head) {
System.out.print(current.data + " ");
current = current.next;
}Solution
Step 1: Analyze loop condition with initial current value
current is initialized to head, so current != head is false at start, loop body never runs.Step 2: Understand correct loop for circular list traversal
Use do-while loop to ensure at least one iteration, since current starts at head.Final Answer:
The loop never executes because current == head initially. -> Option AQuick Check:
while(current != head) false at start [OK]
- Using while instead of do-while for circular lists
- Not printing the first node
- Assuming loop runs when condition false initially
Solution
Step 1: Understand traversal stopping condition for circular list
Traversal must stop when current returns to head to avoid infinite loop.Step 2: Analyze each option's traversal logic
Initialize sum=0; Node current=head; do { sum += current.data; current = current.next; } while(current != head); return sum; uses do-while starting at head, summing all nodes once. Initialize sum=0; Node current=head; while(current != null) { sum += current.data; current = current.next; } return sum; uses while(current != null), which never happens in circular list. Initialize sum=0; Node current=head.next; while(current != head) { sum += current.data; current = current.next; } return sum; starts at head.next, missing head's data. Initialize sum=0; Node current=null; do { sum += current.data; current = current.next; } while(current != head); return sum; initializes current to null causing error.Final Answer:
Initialize sum=0; Node current=head; do { sum += current.data; current = current.next; } while(current != head); return sum; -> Option CQuick Check:
Use do-while with current != head for full traversal [OK]
- Using while(current != null) causing infinite loop
- Skipping head node in sum calculation
- Initializing current to null causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
