Reverse a Singly Linked List Iterative in DSA Java - Time & Space Complexity
We want to understand how the time needed to reverse a singly linked list changes as the list grows.
How does the number of steps grow when the list gets longer?
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 a linked list takes more than linear time because of pointer changes."
[OK] Correct: Each node is visited once and its pointer changed once, so the work grows only linearly with the list size.
Understanding this time complexity helps you explain why reversing a list is efficient and expected to be done in one pass.
"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 from head to tail.Step 2: Purpose of reversing
Reversing changes the links so the last node points to the previous one, making it the new head.Final Answer:
To change the direction of all links so the last node becomes the first -> Option CQuick Check:
Reversing = changing link directions [OK]
- Confusing reversing with sorting
- Thinking reversing deletes nodes
- Assuming reversing adds nodes
Solution
Step 1: Understand the role of previous pointer
Previous pointer starts as null because the new tail's next should be null after reversal.Step 2: Initialization
Setting prev to null ensures the first node processed points to null, ending the reversed list properly.Final Answer:
Node prev = null; -> Option DQuick Check:
Prev starts null to mark new tail [OK]
- Initializing prev to head causes cycle
- Creating new node wastes memory
- Starting prev at head.next skips first node
Solution
Step 1: Trace the reversal process
Initially: 1 -> 2 -> 3 -> null. After reversal, links point backward: 3 -> 2 -> 1 -> null.Step 2: Confirm final head and tail
Node 3 becomes new head, node 1 becomes tail pointing to null.Final Answer:
3 -> 2 -> 1 -> null -> Option BQuick Check:
Reversed list = 3 -> 2 -> 1 [OK]
- Assuming list stays same after reversal
- Confusing null placement
- Mixing node order
Node prev = head;
Node current = head.next;
while (current != null) {
Node nextNode = current.next;
current.next = prev;
prev = current;
current = nextNode;
}
head.next = null;
head = prev;Solution
Step 1: Check initialization of prev pointer
Prev must start as null to mark new tail's next as null after reversal.Step 2: Effect of wrong initialization
Starting prev at head causes a cycle, breaking reversal logic.Final Answer:
prev should be initialized to null, not head -> Option AQuick Check:
Prev = null to avoid cycles [OK]
- Starting prev at head causes infinite loop
- Wrong loop condition misses last node
- Forgetting to set old head next to null
Solution
Step 1: Verify initialization of pointers
Prev must start as null, current at head to reverse all nodes correctly.Step 2: Check loop condition and pointer updates
Loop runs while current != null to process all nodes; inside loop, links reversed properly.Step 3: Confirm final head assignment and printing
After loop, head is set to prev (new head). Printing from head shows reversed list.Final Answer:
Option A code correctly reverses and prints the list -> Option AQuick Check:
Prev=null, loop full list, head=prev [OK]
- Starting prev at head causes cycle
- Loop condition misses last node
- Setting current.next = null inside loop breaks links
Ready to prove it?
Pass the test and this pattern moves unknown
