Reverse a Singly Linked List Iterative in DSA C - Time & Space Complexity
We want to know how the time needed to reverse a linked list changes as the list gets bigger.
Specifically, how many steps does it take to reverse all the nodes one by one?
Analyze the time complexity of the following code snippet.
// Reverse a singly linked list iteratively
struct Node* reverseList(struct Node* head) {
struct Node* prev = NULL;
struct Node* curr = head;
struct Node* next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
This code reverses the links between nodes in a singly linked list one by one until the entire list is reversed.
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.
Each node is visited once, so the total 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: Doubling the list size roughly doubles the work done.
Time Complexity: O(n)
This means the time to reverse the list grows in a straight line 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 in one direction only, from head to tail.Step 2: Purpose of iterative reversal
The iterative method changes the direction of each node's pointer to point to the previous node, reversing the list.Final Answer:
To change the direction of the links between nodes one by one -> Option DQuick Check:
Reversing means changing links direction [OK]
- Thinking reversal creates a new list instead of changing links
- Confusing reversal with sorting
- Assuming nodes are deleted during reversal
Solution
Step 1: Identify starting pointers
For reversal, prev starts as NULL because the new tail points to NULL, current starts at head to traverse.Step 2: Correct initialization
Next pointer is initialized as NULL and updated inside the loop; so initial next = NULL is correct.Final Answer:
struct Node *prev = NULL, *current = head, *next = NULL; -> Option AQuick Check:
Prev NULL, current head to start [OK]
- Setting prev to head initially
- Setting current to NULL at start
- Initializing next pointer incorrectly
Solution
Step 1: Trace reversal process
Initially: 1->2->3->NULL. After reversal, links point backward: 3->2->1->NULL.Step 2: Confirm final head
The last node (3) becomes the new head, so output is 3->2->1->NULL.Final Answer:
3 -> 2 -> 1 -> NULL -> Option BQuick Check:
Reverse order of nodes [OK]
- Assuming list stays the same after reversal
- Confusing NULL placement
- Mixing node order incorrectly
struct Node* reverse(struct Node* head) {
struct Node* prev = head;
struct Node* current = head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}Solution
Step 1: Check pointer initialization
Prev must start as NULL because the new tail node points to NULL, not head.Step 2: Analyze loop pointer updates
Current and next are correctly updated; only prev initialization is wrong.Final Answer:
prev should be initialized to NULL, not head -> Option AQuick Check:
Prev NULL to mark new tail [OK]
- Starting prev at head causing cycle
- Not updating next inside loop
- Returning wrong pointer at end
Solution
Step 1: Reverse the list once
Original: 10->20->30->40->NULL. After first reversal: 40->30->20->10->NULL.Step 2: Reverse the reversed list again
Reversing 40->30->20->10->NULL returns it to original: 10->20->30->40->NULL.Final Answer:
10 -> 20 -> 30 -> 40 -> NULL -> Option CQuick Check:
Double reversal restores original list [OK]
- Assuming double reversal changes list order
- Confusing node values after reversal
- Forgetting NULL at list end
Ready to prove it?
Pass the test and this pattern moves unknown
