Reverse a Singly Linked List Iterative in DSA Typescript - Time & Space Complexity
We want to understand how the time needed to reverse a linked list changes as the list gets longer.
How does the number of steps grow when the list size increases?
Analyze the time complexity of the following code snippet.
function reverseList(head: ListNode | null): ListNode | null {
let prev = null;
let current = head;
while (current !== null) {
const next = current.next;
current.next = prev;
prev = current;
current = next;
}
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 as many times as the list length.
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 changing pointers is a simple step done in constant time per node.
Understanding this helps you explain how to efficiently reverse lists, a common task in coding problems and real applications.
"What if we used recursion instead of iteration? How would the time complexity change?"
Practice
Solution
Step 1: Understand the operation of reversing
Reversing a singly linked list means changing the direction of the next pointers so the last node becomes the first.Step 2: Identify iterative approach
The iterative method changes links step-by-step using a loop without recursion.Final Answer:
To change the direction of the links between nodes without using recursion -> Option BQuick Check:
Reversing iteratively = changing links direction without recursion [OK]
- Confusing reversing with sorting
- Thinking recursion is required
- Assuming nodes are deleted
Solution
Step 1: Understand pointer roles
Prev starts as null because the new tail points to null; current starts at head to traverse.Step 2: Match initialization
Only let prev = null; let current = head; sets prev to null and current to head correctly for iteration.Final Answer:
let prev = null; let current = head; -> Option AQuick Check:
Prev null, current head = correct start [OK]
- Setting prev to head initially
- Starting current as null
- Both pointers null at start
function reverseList(head: ListNode | null): ListNode | null {
let prev = null;
let current = head;
while (current !== null) {
let nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}
return prev;
}Solution
Step 1: Trace the reversal loop
Initially prev=null, current=1. Each iteration reverses current.next to prev and moves pointers forward.Step 2: Final reversed list
After loop, prev points to node 3, which links to 2, then 1, then null.Final Answer:
3 -> 2 -> 1 -> null -> Option CQuick Check:
Reversed list = 3 -> 2 -> 1 -> null [OK]
- Returning head instead of prev
- Not updating current correctly
- Confusing next pointer updates
function reverseList(head: ListNode | null): ListNode | null {
let prev = head;
let current = head.next;
while (current !== null) {
let nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}
return prev;
}Solution
Step 1: Check pointer initialization
Prev must start as null because the new tail points to null, not head.Step 2: Understand impact of wrong initialization
Starting prev at head causes a cycle or incorrect reversal.Final Answer:
prev should be initialized to null, not head -> Option DQuick Check:
Prev must be null at start [OK]
- Starting prev at head causing loops
- Starting current at wrong node
- Returning wrong pointer
Solution
Step 1: Reverse the original list
Original: 10 -> 20 -> 30 -> 40 -> null reversed becomes 40 -> 30 -> 20 -> 10 -> null.Step 2: Insert 25 between 30 and 20
In reversed list, 30 points to 20, so insert 25 after 30: 30 -> 25 -> 20.Step 3: Final list order
40 -> 30 -> 25 -> 20 -> 10 -> null.Final Answer:
40 -> 30 -> 25 -> 20 -> 10 -> null -> Option AQuick Check:
Insert between 30 and 20 in reversed list = 40->30->25->20->10 [OK]
- Inserting 25 between wrong nodes after reversal
- Confusing original and reversed order
- Not updating next pointers correctly
Ready to prove it?
Pass the test and this pattern moves unknown
