Reverse a Singly Linked List Iterative in DSA Javascript - Time & Space Complexity
We want to know how the time to reverse a linked list changes as the list gets bigger.
How many steps does it take to reverse all the links in the list?
Analyze the time complexity of the following code snippet.
function reverseLinkedList(head) {
let prev = null;
let current = head;
while (current !== null) {
let next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
This code reverses the links of a singly linked list by changing each node's next pointer.
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.
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: Doubling the list size roughly doubles the work done.
Time Complexity: O(n)
This means the time to reverse the list grows linearly with the number of nodes.
[X] Wrong: "Reversing a linked list takes constant time because we just change pointers."
[OK] Correct: Each node's pointer must be changed one by one, so the time depends on the list size.
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
head -> 1 -> 2 -> 3 -> null becomes?Solution
Step 1: Understand the linked list order
The original list goes from 1 to 3 in order: 1 -> 2 -> 3 -> null.Step 2: Reverse the pointers iteratively
Reversing means the last node becomes first, so the list becomes 3 -> 2 -> 1 -> null.Final Answer:
head -> 3 -> 2 -> 1 -> null -> Option AQuick Check:
Reverse order = head -> 3 -> 2 -> 1 -> null [OK]
- Thinking the list stays the same after reverse
- Confusing node values with pointers
- Assuming null moves to front
Solution
Step 1: Initialize previous pointer to null
Before reversal, previous node is null because head has no previous.Step 2: Initialize current pointer to head
Current starts at head to traverse the list.Final Answer:
let prev = null, current = head; -> Option DQuick Check:
Start prev=null, current=head [OK]
- Setting prev to head initially
- Setting current to null at start
- Swapping initialization order
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
function reverseList(head) {
let prev = null;
let current = head;
while (current) {
let nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}
return prev;
}
// Create list: 1 -> 2 -> 3 -> null
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
let reversedHead = reverseList(head);
let result = [];
let node = reversedHead;
while (node) {
result.push(node.val);
node = node.next;
}
console.log(result);Solution
Step 1: Trace reversal loop
Each iteration reverses the next pointer: 1->2->3 becomes 3->2->1.Step 2: Collect values from reversed list
After reversal, traversing from prev (new head) gives values 3, 2, 1.Final Answer:
[3, 2, 1] -> Option BQuick Check:
Reversed list values = [3, 2, 1] [OK]
- Returning original head instead of prev
- Not updating current pointer correctly
- Collecting values before reversal
function reverseList(head) {
let prev = head;
let current = head.next;
while (current !== null) {
let nextTemp = current.next;
current.next = prev;
prev = current;
current = nextTemp;
}
head.next = null;
return prev;
}Solution
Step 1: Check initial pointer setup
prev must start as null because head has no previous node before reversal.Step 2: Understand impact of wrong prev initialization
Starting prev as head causes a cycle or incorrect links during reversal.Final Answer:
prev should start as null, not head -> Option CQuick Check:
prev=null start fixes reversal [OK]
- Starting prev at head causing loops
- Not setting head.next to null after reversal
- Changing nextTemp after modifying current.next
1 -> 2 -> 3 -> 4 -> 5 -> null, write an iterative function to reverse only the first 3 nodes. What will be the list after reversal?Solution
Step 1: Reverse first 3 nodes iteratively
Reverse nodes 1, 2, 3 to get 3 -> 2 -> 1.Step 2: Connect reversed part to remaining list
Node 1 (now last in reversed part) points to node 4, preserving rest of list.Final Answer:
3 -> 2 -> 1 -> 4 -> 5 -> null -> Option AQuick Check:
Partial reversal keeps rest intact [OK]
- Reversing entire list instead of first 3 nodes
- Not reconnecting reversed part to rest
- Breaking list by losing tail connections
Ready to prove it?
Pass the test and this pattern moves unknown
