What is the base case in the recursive approach to reverse a singly linked list?
The base case is when the current node is null or its next node is null, meaning we have reached the end or the list is empty. At this point, the recursion stops and returns the current node as the new head.
Click to reveal answer
intermediate
In reversing a singly linked list recursively, what happens after the recursive call returns?
After the recursive call returns the new head, the next node's next pointer is set to the current node, effectively reversing the link between them. Then, the current node's next pointer is set to null to avoid cycles.
Click to reveal answer
beginner
Why do we set the current node's next pointer to null during recursion when reversing a singly linked list?
Setting the current node's next pointer to null breaks the original forward link, preventing cycles and ensuring the reversed list ends properly.
Click to reveal answer
beginner
Show the TypeScript function signature for recursively reversing a singly linked list node.
function reverseList(head: ListNode | null): ListNode | null
Click to reveal answer
beginner
What is the time complexity of reversing a singly linked list recursively?
The time complexity is O(n), where n is the number of nodes, because each node is visited once during the recursion.
Click to reveal answer
What does the base case in recursive linked list reversal check for?
AIf the current node is null or next is null
BIf the list has more than two nodes
CIf the current node's value is zero
DIf the list is already reversed
✗ Incorrect
The base case stops recursion when the current node is null or its next is null, indicating the end or empty list.
After recursion returns, what pointer is changed to reverse the link?
ACurrent node's next pointer
BTail pointer
CHead pointer
DNext node's next pointer
✗ Incorrect
The next node's next pointer is set to the current node to reverse the link.
Why do we set current node's next to null in recursive reversal?
ATo speed up recursion
BTo avoid cycles in the reversed list
CTo mark the node as visited
DTo delete the node
✗ Incorrect
Setting current node's next to null breaks the original link and prevents cycles.
What is the time complexity of recursive singly linked list reversal?
AO(n)
BO(n^2)
CO(log n)
DO(1)
✗ Incorrect
Each node is visited once, so time complexity is linear O(n).
What is the return value of the recursive reverse function?
ATail node
BOriginal head
CNew head of the reversed list
DNull
✗ Incorrect
The function returns the new head node of the reversed list.
Explain step-by-step how recursion reverses a singly linked list.
Think about how the links between nodes are flipped during the return phase of recursion.
You got /5 concepts.
Write the TypeScript code for recursively reversing a singly linked list and explain each line.
Focus on how the pointers change after the recursive call returns.
You got /6 concepts.
Practice
(1/5)
1. What is the main idea behind reversing a singly linked list using recursion?
easy
A. Create a new list and copy nodes in reverse order
B. Swap the values of the first and last nodes only
C. Reverse the rest of the list first, then fix the current node's next pointer
D. Iterate through the list and reverse pointers using a loop
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 C
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 A
Quick Check:
Reversed list = 3 -> 2 -> 1 [OK]
Hint: Last node becomes new head after recursion [OK]
Common Mistakes:
Not setting head.next to null causing cycles
Returning original head instead of new head
Confusing node values with pointers
4. Identify the bug in this recursive reverse function and choose the fix:
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;
}
medium
A. Remove the base case for head.next === null
B. Swap the last two pointer assignments: set head.next.next before head.next = null
C. Return head instead of newHead
D. Add a loop to reverse pointers instead of recursion
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 B
Quick Check:
Fix pointer order to avoid null errors [OK]
Hint: Set head.next.next before nullifying head.next [OK]
Common Mistakes:
Nullifying head.next too early
Ignoring base cases
Returning wrong node
5. Given a singly linked list with nodes containing values [10, 20, 30, 40, 50], which recursive approach correctly reverses the list and returns the new head?