Traversal of Circular Linked List in DSA C - Time & Space Complexity
We want to understand how long it takes to visit every node in a circular linked list.
How does the time needed grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
void traverseCircularList(Node* head) {
if (head == NULL) return;
Node* current = head;
do {
printf("%d -> ", current->data);
current = current->next;
} while (current != head);
printf("NULL\n");
}
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 visits grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits |
| 100 | About 100 visits |
| 1000 | About 1000 visits |
Pattern observation: The time grows in a straight line 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 list takes infinite time because it loops forever."
[OK] Correct: The loop stops when it returns to the starting node, so it visits each node only once.
Understanding traversal time helps you explain how circular lists work and shows you can reason about loops that cycle back.
"What if we changed the traversal to stop after visiting only half the nodes? How would the time complexity change?"
Practice
Solution
Step 1: Understand traversal stopping condition for singly linked list
In a singly linked list, traversal stops when the next pointer is NULL, indicating the end.Step 2: Understand traversal stopping condition for circular linked list
In a circular linked list, there is no NULL next pointer; traversal stops when the starting node is reached again.Final Answer:
In a circular linked list, traversal stops when the starting node is reached again. -> Option BQuick Check:
Traversal stops at start node in circular list [OK]
- Assuming NULL marks end in circular list
- Stopping traversal too early
- Confusing circular with doubly linked list
- Not checking for start node to stop
Solution
Step 1: Identify correct loop structure for circular linked list traversal
Traversal must start at head and continue until we reach head again, so do-while loop is suitable.Step 2: Check each option's loop condition
Node* temp = head; do { /* process temp */ temp = temp->next; } while(temp != head); uses do-while with condition temp != head, which correctly traverses all nodes once.Final Answer:
Node* temp = head; do { /* process temp */ temp = temp->next; } while(temp != head); -> Option CQuick Check:
Use do-while with temp != head for circular list [OK]
- Using while(temp != NULL) in circular list
- Using for loop without length variable
- Checking temp->next != NULL which never happens
- Starting traversal without processing head
Node* temp = head;
do {
printf("%d ", temp->data);
temp = temp->next;
} while(temp != head);Solution
Step 1: Understand traversal order in circular linked list
Traversal starts at head (10), prints data, moves to next node until it reaches head again.Step 2: Trace the loop execution
Prints 10, moves to 20, prints 20, moves to 30, prints 30, moves back to 10, loop ends.Final Answer:
10 20 30 -> Option AQuick Check:
Print nodes once until back to head [OK]
- Printing extra 10 at end
- Starting from second node
- Stopping before last node
- Confusing circular with linear list output
Node* temp = head;
while(temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
if(temp == head) break;
}Solution
Step 1: Analyze loop condition for circular linked list
In circular linked list, next pointer is never NULL, so while(temp != NULL) will cause infinite loop.Step 2: Check break condition usage
Break condition temp == head is correct to stop traversal after full cycle.Final Answer:
The loop condition while(temp != NULL) is wrong for circular lists. -> Option AQuick Check:
Use do-while or check for start node, not NULL [OK]
- Using while(temp != NULL) causing infinite loop
- Incorrect break condition
- Printing after moving temp
- Confusing linear and circular list traversal
Node* temp = head;
do {
// print temp->data
temp = temp->next;
} while(temp != head);Solution
Step 1: Understand requirement to skip printing node with value 25
We want to traverse all nodes but not print the node with data 25.Step 2: Modify printing logic inside traversal loop
Add an if condition to print only if temp->data is not 25, so node is visited but not printed.Final Answer:
Add an if condition: if(temp->data != 25) printf("%d ", temp->data); -> Option DQuick Check:
Use if condition to skip printing specific node [OK]
- Modifying links during traversal causing list corruption
- Breaking loop early missing nodes
- Starting traversal from wrong node
- Printing all nodes without condition
Ready to prove it?
Pass the test and this pattern moves unknown
