Reverse a Singly Linked List Iterative in DSA C# - Time & Space Complexity
We want to understand how the time needed to reverse a singly linked list changes as the list gets bigger.
Specifically, how many steps does it take to reverse all the nodes?
Analyze the time complexity of the following code snippet.
public ListNode ReverseList(ListNode head) {
ListNode prev = null;
ListNode current = head;
while (current != null) {
ListNode nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}
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 for each node in the list (n times).
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 reverse the list grows directly with the number of nodes.
[X] Wrong: "Reversing the list takes more time because of multiple pointer changes inside the loop."
[OK] Correct: Each node is visited only once, and pointer changes are constant time operations, so the total time still grows linearly.
Understanding this linear time reversal is a key skill that shows you can work with linked lists efficiently and confidently.
"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 through next pointers.Step 2: Purpose of iterative reversal
The iterative method changes each node's next pointer to point backward, reversing the list.Final Answer:
To change the direction of the links between nodes one by one -> Option CQuick Check:
Reversing = changing links direction [OK]
- Thinking reversal deletes nodes
- Confusing reversal with sorting
- Assuming reversal adds nodes
Solution
Step 1: Initialization for reversal
We start with prev as null because the new tail will point to null, and current at head to traverse.Step 2: Check each option
Only Node prev = null; Node current = head; correctly sets prev to null and current to head for iteration.Final Answer:
Node prev = null; Node current = head; -> Option DQuick Check:
Start prev=null, current=head [OK]
- Setting prev to head initially
- Setting current to null
- Both pointers null at start
Node prev = null;
Node current = head;
while (current != null) {
Node nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}
head = prev;Solution
Step 1: Trace the reversal loop
Each iteration reverses the link: 1 points to null, 2 points to 1, 3 points to 2.Step 2: Final linked list after loop
After loop, prev points to 3, which is new head: 3 -> 2 -> 1 -> null.Final Answer:
3 -> 2 -> 1 -> null -> Option BQuick Check:
Reversed list = 3->2->1 [OK]
- Assuming list stays same
- Forgetting to update head
- Confusing next and prev pointers
Node prev = head;
Node current = head.next;
while (current != null) {
Node nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}
head.next = null;
head = prev;Solution
Step 1: Check pointer initialization
prev must start as null to mark new tail's next as null; starting at head causes cycle.Step 2: Effect of wrong initialization
Starting prev at head causes the first node to point to itself, creating a loop.Final Answer:
prev should be initialized to null, not head -> Option AQuick Check:
Prev must be null at start [OK]
- Starting prev at head causing loops
- Not setting head.next to null after reversal
- Starting current at wrong node
Solution
Step 1: First reversal
Reversing 10->20->30->40 gives 40->30->20->10->null.Step 2: Second reversal
Reversing again restores original order: 10->20->30->40->null.Final Answer:
10 -> 20 -> 30 -> 40 -> null -> Option AQuick Check:
Double reversal restores original list [OK]
- Assuming second reversal changes order again
- Confusing node values after reversal
- Forgetting reversal is reversible
Ready to prove it?
Pass the test and this pattern moves unknown
