Traversal and Printing a Linked List in DSA C - Time & Space Complexity
When we traverse and print a linked list, we want to know how the time it takes changes as the list grows.
We ask: How does the number of steps grow when the list gets longer?
Analyze the time complexity of the following code snippet.
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
This code goes through each node in the linked list and prints its value until it reaches the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that visits each node once.
- How many times: Exactly once per node, so as many times as there are nodes in the list.
As the list gets longer, the number of steps grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps increase in a straight line as the list size grows.
Time Complexity: O(n)
This means the time to print the list grows directly with the number of nodes.
[X] Wrong: "The printing takes constant time because it just prints once."
[OK] Correct: Actually, it prints once for each node, so the time grows with the list size, not fixed.
Understanding this helps you explain how simple list operations scale, a key skill in many coding interviews.
"What if we changed the linked list to a doubly linked list? How would the time complexity change when printing all nodes?"
Practice
Solution
Step 1: Understand traversal meaning
Traversal means visiting each node one by one in the linked list.Step 2: Identify traversal purpose
The main goal is to access or print the data stored in each node.Final Answer:
To visit each node and access its data -> Option BQuick Check:
Traversal = Visit nodes [OK]
- Confusing traversal with deletion
- Thinking traversal creates new nodes
- Assuming traversal reverses the list
head in C?Solution
Step 1: Understand pointer usage
In C, linked list nodes are accessed via pointers, sotempmust be a pointer toNode.Step 2: Check syntax correctness
struct Node* temp = head; correctly declarestempas a pointer and assigns it tohead.Final Answer:
struct Node* temp = head; -> Option CQuick Check:
Pointer assignment = struct Node* temp = head [OK]
- Using struct Node instead of pointer
- Taking address of head incorrectly
- Missing struct keyword if needed
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node n3 = {30, NULL};
struct Node n2 = {20, &n3};
struct Node n1 = {10, &n2};
struct Node* head = &n1;
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
return 0;
}Solution
Step 1: Trace linked list nodes
The list nodes are n1(10) -> n2(20) -> n3(30) -> NULL.Step 2: Follow traversal and print
The loop prints each node's data followed by ' -> ', then prints 'NULL' at the end.Final Answer:
10 -> 20 -> 30 -> NULL -> Option AQuick Check:
Print nodes in order = 10 -> 20 -> 30 -> NULL [OK]
- Printing nodes in reverse order
- Missing '->' arrows in output
- Forgetting to print NULL at end
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* head) {
while (head->next != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("NULL\n");
}Solution
Step 1: Analyze loop condition
The loop stops when head->next is NULL, so it skips printing the last node.Step 2: Correct loop condition
Changing condition to head != NULL ensures all nodes, including last, are printed.Final Answer:
The loop condition should be head != NULL -> Option DQuick Check:
Loop until head is NULL to print all nodes [OK]
- Stopping loop too early
- Using wrong pointer type
- Ignoring last node in print
struct Node* temp = head;
while (temp) {
if (temp->data) {
printf("%d -> ", temp->data);
}
temp = temp->next;
}
printf("NULL\n");Solution
Step 1: Understand the if condition effect
The condition 'if (temp->data)' skips printing nodes with data 0 because 0 is false.Step 2: Fix to print all nodes
Removing the if condition prints all nodes including those with 0.Final Answer:
Change 'if (temp->data)' to always print without condition -> Option AQuick Check:
Print all nodes regardless of data value [OK]
- Skipping zero values due to if condition
- Moving pointer only inside if block causing infinite loop
- Using wrong print function for integers
Ready to prove it?
Pass the test and this pattern moves unknown
