Reverse a Singly Linked List Recursive in DSA Typescript - Time & Space Complexity
We want to understand how the time needed to reverse a linked list grows as the list gets longer.
Specifically, how does the recursive method handle each node as the list size increases?
Analyze the time complexity of the following code snippet.
function reverseList(head: ListNode | null): ListNode | null {
if (head === null || head.next === null) return head;
const newHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
This code reverses a singly linked list by calling itself on the next node until it reaches the end, then rewires the links backward.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call on each node of the list.
- How many times: Once per node, so exactly n times for a list of size n.
Each node causes one recursive call, so the total steps grow directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 recursive calls and rewiring steps |
| 100 | About 100 recursive calls and rewiring steps |
| 1000 | About 1000 recursive calls and rewiring steps |
Pattern observation: The work grows linearly as the list size grows.
Time Complexity: O(n)
This means the time to reverse the list grows directly in proportion to the number of nodes.
[X] Wrong: "Recursive reversal is faster because it uses fewer lines of code."
[OK] Correct: The number of steps depends on the list size, not code length. Both recursive and iterative methods visit each node once, so time grows the same way.
Understanding this recursive approach shows you can think about problems in different ways and handle linked lists confidently, a key skill in many coding challenges.
"What if we changed the recursion to an iterative loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand recursion on linked list
Recursion works by solving a smaller part first, here reversing the rest of the list beyond the current node.Step 2: Fix current node's pointer after recursion
After the rest is reversed, the current node's next node points back to it, reversing the link.Final Answer:
Reverse the rest of the list first, then fix the current node's next pointer -> Option CQuick Check:
Recursive reversal = reverse rest + fix pointer [OK]
- Trying to swap node values instead of pointers
- Not fixing the current node's next pointer after recursion
- Using iteration instead of recursion
Solution
Step 1: Identify when recursion should stop
The base case is when the list is empty or has only one node, so no reversal needed.Step 2: Check the correct base case syntax
Return the head if it is null or its next is null, meaning end or single node.Final Answer:
if (head === null || head.next === null) return head; -> Option DQuick Check:
Base case = empty or single node [OK]
- Returning null when only one node exists
- Accessing head.next without checking if head is null
- Incorrectly returning head.next or head.next.next
function reverseList(head: ListNode | null): ListNode | null {
if (!head || !head.next) return head;
const newHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return newHead;
}Solution
Step 1: Trace recursion on list 1 -> 2 -> 3
Recursion goes to node 3 (base case), then reverses links back: 3 points to 2, 2 points to 1.Step 2: Update pointers to reverse list
head.next.next = head sets 2's next to 1, head.next = null breaks old link, final reversed list is 3 -> 2 -> 1 -> null.Final Answer:
3 -> 2 -> 1 -> null -> Option AQuick Check:
Reversed list = 3 -> 2 -> 1 [OK]
- Not setting head.next to null causing cycles
- Returning original head instead of new head
- Confusing node values with pointers
function reverseList(head: ListNode | null): ListNode | null {
if (!head) return null;
if (!head.next) return head;
const newHead = reverseList(head.next);
head.next = null;
head.next.next = head;
return newHead;
}Solution
Step 1: Analyze pointer assignments order
Setting head.next = null before head.next.next = head breaks the chain because head.next becomes null.Step 2: Correct order to fix pointers
First set head.next.next = head to reverse link, then set head.next = null to break old link.Final Answer:
Swap the last two pointer assignments: set head.next.next before head.next = null -> Option BQuick Check:
Fix pointer order to avoid null errors [OK]
- Nullifying head.next too early
- Ignoring base cases
- Returning wrong node
function reverseList(head: ListNode | null): ListNode | null {
if (!head || !head.next) return head;
const newHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
What will be the final linked list after calling reverseList on the head node with value 10?Solution
Step 1: Understand recursive reversal on list [10,20,30,40,50]
The recursion reaches the last node (50), then reverses links step-by-step back to the first node.Step 2: Trace pointer changes during recursion unwind
Each node's next node points back to it, and its next pointer is set to null, resulting in reversed list 50 -> 40 -> 30 -> 20 -> 10 -> null.Final Answer:
50 -> 40 -> 30 -> 20 -> 10 -> null -> Option AQuick Check:
Recursive reverse flips list order [OK]
- Expecting original order after reversal
- Not setting head.next to null causing cycles
- Confusing node values with pointers
Ready to prove it?
Pass the test and this pattern moves unknown
