Reverse a Singly Linked List Iterative in DSA C++ - Time & Space Complexity
We want to know how the time to reverse a linked list changes as the list gets bigger.
How does the number of steps grow when the list length increases?
Analyze the time complexity of the following code snippet.
Node* reverseList(Node* head) {
Node* prev = nullptr;
Node* current = head;
while (current != nullptr) {
Node* nextNode = current->next;
current->next = prev;
prev = current;
current = nextNode;
}
return prev;
}
This code reverses a singly linked list by changing the direction of the links one by one.
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 n times for n nodes.
Each node is visited once, so the steps grow 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 time grows in a straight line as the list gets longer.
Time Complexity: O(n)
This means the time to reverse the list grows directly with the number of nodes.
[X] Wrong: "Reversing a linked list takes more than linear time because of pointer changes."
[OK] Correct: Each node is visited only once, and pointer changes happen in constant time per node, so total time is linear.
Understanding this linear time reversal is a key skill that shows you can work with linked lists efficiently.
"What if we used recursion instead of iteration? How would the time complexity change?"
Practice
Solution
Step 1: Understand the linked list structure
A singly linked list has nodes connected by pointers going one way only.Step 2: Purpose of iterative reversal
The iterative method changes each node's pointer to point backward, reversing the list.Final Answer:
To change the direction of the links between nodes one by one -> Option BQuick Check:
Reversing = Changing links direction [OK]
- Confusing reversal with deletion
- Thinking it sorts the list
- Assuming it creates a new list
Solution
Step 1: Understand pointer initialization
In C++, pointers are declared with * and can be set to NULL or nullptr.Step 2: Correct pointer declaration
"Node* prev = NULL;" correctly declares a pointer named prev and initializes it to NULL.Final Answer:
Node* prev = NULL; -> Option AQuick Check:
Pointer prev initialized to NULL [OK]
- Declaring pointer without *
- Assigning NULL to non-pointer variable
- Using 0 without pointer declaration
Solution
Step 1: Trace reversal of 1 -> 2 -> 3
Start with prev = NULL, curr = 1. Change 1's next to NULL, move prev to 1, curr to 2.Step 2: Continue reversing links
Change 2's next to 1, move prev to 2, curr to 3. Then change 3's next to 2, prev to 3, curr to NULL.Final Answer:
3 -> 2 -> 1 -> null -> Option AQuick Check:
Reversed list = 3 -> 2 -> 1 [OK]
- Not updating pointers correctly
- Leaving last node pointing to old next
- Confusing original and reversed order
Node* reverse(Node* head) {
Node* prev = NULL;
Node* curr = head;
while (curr != NULL) {
Node* next = curr->next;
curr->next = prev;
prev = curr;
// Missing update of curr here
}
return prev;
}Solution
Step 1: Check loop pointer updates
Inside the loop, curr must move to next node to avoid infinite loop.Step 2: Identify missing update
The code misses 'curr = next;' causing curr to never change and loop forever.Final Answer:
The pointer curr is never updated inside the loop, causing an infinite loop -> Option DQuick Check:
Missing curr update = infinite loop [OK]
- Forgetting to update curr
- Returning wrong pointer
- Not initializing prev
Solution
Step 1: Check pointer initialization and loop condition
Node* reverseList(Node* head) { Node* prev = nullptr; Node* curr = head; while (curr) { Node* next = curr->next; curr->next = prev; prev = curr; curr = next; } return prev; } initializes prev to nullptr, curr to head, and loops while curr is not null.Step 2: Verify pointer updates inside loop
Node* reverseList(Node* head) { Node* prev = nullptr; Node* curr = head; while (curr) { Node* next = curr->next; curr->next = prev; prev = curr; curr = next; } return prev; } saves next, reverses curr->next, moves prev and curr forward correctly, then returns prev as new head.Final Answer:
Node* reverseList(Node* head) { Node* prev = nullptr; Node* curr = head; while (curr) { Node* next = curr->next; curr->next = prev; prev = curr; curr = next; } return prev; } -> Option CQuick Check:
Correct pointer updates and return [OK]
- Wrong loop condition
- Not saving next before changing links
- Returning wrong pointer
Ready to prove it?
Pass the test and this pattern moves unknown
