Traversal and Printing a Linked List in DSA C++ - Time & Space Complexity
We want to understand how long it takes to go through all the items in a linked list and print them.
The question is: how does the time needed grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
struct Node {
int data;
Node* next;
};
void printList(Node* head) {
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " -> ";
current = current->next;
}
std::cout << "null" << std::endl;
}
This code goes through each node in the linked list from the start 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 for each node in the list, from first to last.
As the list gets longer, the time to print grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 visits and prints |
| 100 | About 100 visits and prints |
| 1000 | About 1000 visits and prints |
Pattern observation: The time grows in a straight line as the list size increases.
Time Complexity: O(n)
This means the time to print grows directly in proportion to the number of nodes in the list.
[X] Wrong: "Printing a linked list is faster than printing an array because it uses pointers."
[OK] Correct: Both linked lists and arrays require visiting each element once to print, so they take similar time proportional to their size.
Understanding how to traverse and print a linked list efficiently is a basic but important skill that shows you know how to work with pointers and data structures.
"What if we changed the linked list to a doubly linked list? How would the time complexity of printing change?"
Practice
Solution
Step 1: Understand traversal meaning
Traversal means visiting each node one by one in the linked list.Step 2: Purpose of traversal
The main goal is to access or process the data stored in each node during the visit.Final Answer:
To visit each node and access its data -> Option DQuick Check:
Traversal = Visit nodes [OK]
- Confusing traversal with deletion
- Thinking traversal creates new lists
- Assuming traversal reverses the list
head is the pointer to the first node?Solution
Step 1: Understand pointer usage
In C++, linked list nodes are accessed via pointers.headis a pointer to the first node.Step 2: Correct pointer assignment
To traverse, we create a pointercurrentand assign it toheaddirectly:Node* current = head;Final Answer:
Node* current = head; -> Option AQuick Check:
Use pointer to start traversal [OK]
- Using object instead of pointer
- Taking address of head incorrectly
- Dereferencing head wrongly
struct Node {
int data;
Node* next;
};
int main() {
Node n3 = {3, nullptr};
Node n2 = {2, &n3};
Node n1 = {1, &n2};
Node* head = &n1;
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " -> ";
current = current->next;
}
std::cout << "null" << std::endl;
return 0;
}Solution
Step 1: Trace node connections
Node n1 points to n2, n2 points to n3, n3 points to nullptr (end).Step 2: Follow the while loop output
The loop prints data of current node then moves to next. So prints 1, then 2, then 3, then stops.Final Answer:
1 -> 2 -> 3 -> null -> Option CQuick Check:
Print nodes in order from head [OK]
- Printing nodes in reverse order
- Missing '->' arrows in output
- Not printing 'null' at the end
struct Node {
int data;
Node* next;
};
void printList(Node* head) {
Node* current = head;
while (current->next != nullptr) {
std::cout << current->data << " -> ";
current = current->next;
}
std::cout << "null" << std::endl;
}Solution
Step 1: Analyze loop condition
The loop stops when current->next is nullptr, so it skips printing the last node's data.Step 2: Correct loop condition
It should check current != nullptr to print all nodes including the last one.Final Answer:
The loop condition should be current != nullptr, not current->next != nullptr -> Option AQuick Check:
Loop until current is nullptr to print all nodes [OK]
- Stopping loop too early missing last node
- Wrong pointer type declaration
- Misplacing print statement
Solution
Step 1: Understand constraints
We cannot change the list or use recursion, so reversing pointers or recursive calls are not allowed.Step 2: Use auxiliary data structure
Use a stack to push each node's data during traversal, then pop from stack to print in reverse order.Final Answer:
Use a stack to store nodes during traversal, then pop and print -> Option BQuick Check:
Stack reverses order without modifying list [OK]
- Trying to reverse list pointers directly
- Using recursion despite restriction
- Assuming two pointers can print backwards
Ready to prove it?
Pass the test and this pattern moves unknown
