What is the base case in the recursive reversal of a singly linked list?
The base case is when the current node is null or the next node is null, meaning we have reached the end of the list or the list is empty.
Click to reveal answer
intermediate
In recursive reversal, what happens after the recursive call returns?
After the recursive call returns, the next node's next pointer is set to the current node, reversing the link between them.
Click to reveal answer
intermediate
Why do we set the current node's next pointer to null in recursive reversal?
We set the current node's next pointer to null to avoid cycles and mark the new end of the reversed list.
Click to reveal answer
beginner
Show the Go function signature for recursive reversal of a singly linked list node.
func reverseList(head *ListNode) *ListNode
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.
Click to reveal answer
What does the recursive function return when reversing a singly linked list?
AThe original head of the list
BThe new head of the reversed list
CThe last node visited
DNull
✗ Incorrect
The recursive function returns the new head of the reversed list after all nodes are processed.
Which pointer is changed to reverse the link between two nodes in recursion?
ACurrent node's next pointer
BHead pointer
CNext node's next pointer
DPrevious node's next pointer
✗ Incorrect
The next node's next pointer is set to the current node to reverse the link.
What is the stopping condition for the recursive reversal?
AWhen the current node is null or next node is null
BWhen the current node is the head
CWhen the list has only one node
DWhen the previous node is null
✗ Incorrect
The recursion stops when the current node is null or the next node is null, indicating the end of the list.
What happens to the original head node after reversal?
AIt becomes the tail with next pointer set to null
BIt remains the head
CIt is deleted
DIt points to itself
✗ Incorrect
After reversal, the original head becomes the tail and its next pointer is set to null.
What is the space complexity of recursive reversal of a singly linked list?
AO(log n)
BO(1) constant space
CO(n^2)
DO(n) due to call stack
✗ Incorrect
Recursive calls use the call stack, so space complexity is O(n) for n nodes.
Explain step-by-step how recursion reverses a singly linked list.
Think about what happens when recursion reaches the end and then unwinds.
You got /5 concepts.
Write the Go code for recursive reversal of a singly linked list and explain each line.
Focus on how pointers change and how recursion returns the new head.
You got /5 concepts.
Practice
(1/5)
1. What does the recursive function to reverse a singly linked list do at each step?
easy
A. It reverses the rest of the list and then attaches the current node at the end.
B. It swaps the first two nodes and stops.
C. It deletes the current node and moves to the next.
D. It creates a new list and copies nodes in reverse order.
Solution
Step 1: Understand recursion on linked list
The function calls itself on the next node until it reaches the last node.
Step 2: Attach current node after reversing rest
After recursion returns, it sets the next node's next pointer to current node, reversing the link.
Final Answer:
It reverses the rest of the list and then attaches the current node at the end. -> Option A
Quick Check:
Recursive reversal = attach current after reversed rest [OK]
Hint: Think: reverse rest first, then fix current link [OK]
Common Mistakes:
Stopping recursion too early
Not changing next pointers correctly
Trying to reverse by swapping values
2. Which of the following is the correct base case for a recursive function reversing a singly linked list in Go?
easy
A. if head == nil { return nil }
B. if head == nil || head.Next == nil { return head }
C. if head.Next == nil { return nil }
D. if head == nil && head.Next == nil { return nil }
Solution
Step 1: Identify base case for recursion
The recursion stops when the list is empty or has one node, so head is nil or head.Next is nil.
Step 2: Return head in base case
Return the head node itself because a single node or empty list is already reversed.
Final Answer:
if head == nil || head.Next == nil { return head } -> Option B
Quick Check:
Base case = empty or single node returns head [OK]
Hint: Base case: empty or single node returns itself [OK]
Common Mistakes:
Returning nil for single node
Using && instead of || in condition
Missing base case causing infinite recursion
3. What is the output linked list after running this recursive reverse function on the list 1 -> 2 -> 3 -> nil?
func reverse(head *Node) *Node {
if head == nil || head.Next == nil {
return head
}
newHead := reverse(head.Next)
head.Next.Next = head
head.Next = nil
return newHead
}
medium
A. nil
B. 1 -> 2 -> 3 -> nil
C. 2 -> 3 -> 1 -> nil
D. 3 -> 2 -> 1 -> nil
Solution
Step 1: Trace recursion calls
Calls reverse on 2 -> 3 -> nil, then on 3 -> nil (base case returns 3).
Step 2: Reverse links on return
Sets 3.Next to 2, 2.Next to 1, and 1.Next to nil, reversing the list.
Final Answer:
3 -> 2 -> 1 -> nil -> Option D
Quick Check:
Reversed list = 3->2->1 [OK]
Hint: Follow recursion bottom-up to see reversed links [OK]
Common Mistakes:
Not setting head.Next to nil causing cycle
Returning wrong node as new head
Confusing order of pointer assignments
4. Identify the bug in this recursive reverse function for a singly linked list:
func reverse(head *Node) *Node {
if head == nil {
return nil
}
newHead := reverse(head.Next)
head.Next.Next = head
return newHead
}
medium
A. head.Next.Next should be head.Next
B. Incorrect return type
C. Missing base case for single node causing nil pointer dereference
D. No bug, code is correct
Solution
Step 1: Check base case correctness
Base case only checks if head is nil, but not if head.Next is nil, causing head.Next.Next to panic if head.Next is nil.
Step 2: Identify nil pointer dereference
When head.Next is nil, head.Next.Next causes runtime error because head.Next is nil.
Final Answer:
Missing base case for single node causing nil pointer dereference -> Option C
Quick Check:
Base case must handle single node to avoid nil panic [OK]
Hint: Always check base case for nil and single node [OK]
Common Mistakes:
Ignoring single node base case
Assuming head.Next is never nil
Not setting head.Next to nil after reversal
5. Given a singly linked list with nodes containing values 10 -> 20 -> 30 -> 40 -> nil, what will be the list after applying this recursive reverse function?
func reverse(head *Node) *Node {
if head == nil || head.Next == nil {
return head
}
newHead := reverse(head.Next)
head.Next.Next = head
head.Next = nil
return newHead
}
Choose the correct final linked list.
hard
A. 40 -> 30 -> 20 -> 10 -> nil
B. 10 -> 20 -> 30 -> 40 -> nil
C. 40 -> 10 -> 20 -> 30 -> nil
D. 30 -> 20 -> 10 -> 40 -> nil
Solution
Step 1: Recursively reverse from tail
Recursion reaches node with value 40 (base case), then reverses links back up.
Step 2: Re-link nodes in reverse order
Each node's next node points back to itself, and original head's next is set to nil.
Final Answer:
40 -> 30 -> 20 -> 10 -> nil -> Option A
Quick Check:
Reversed list = 40->30->20->10 [OK]
Hint: Reverse order means last node becomes new head [OK]
Common Mistakes:
Forgetting to set head.Next to nil
Returning old head instead of new head
Misordering nodes after reversal
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.