Challenge - 5 Problems
Recursive Reverse Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Reverse on a 3-Node List
What is the printed state of the linked list after recursively reversing it?
DSA Go
package main import "fmt" type Node struct { val int next *Node } func reverseRecursive(head *Node) *Node { if head == nil || head.next == nil { return head } newHead := reverseRecursive(head.next) head.next.next = head head.next = nil return newHead } func printList(head *Node) { for head != nil { fmt.Printf("%d -> ", head.val) head = head.next } fmt.Println("null") } func main() { head := &Node{val: 1} head.next = &Node{val: 2} head.next.next = &Node{val: 3} head = reverseRecursive(head) printList(head) }
Attempts:
2 left
💡 Hint
Think about how the recursion reverses the links starting from the end.
✗ Incorrect
The recursive function reverses the list by moving to the end and then rewiring the next pointers backward. The final printed list is reversed order.
❓ Predict Output
intermediateOutput of Recursive Reverse on a Single Node List
What is the printed state of the linked list after recursively reversing a single-node list?
DSA Go
package main import "fmt" type Node struct { val int next *Node } func reverseRecursive(head *Node) *Node { if head == nil || head.next == nil { return head } newHead := reverseRecursive(head.next) head.next.next = head head.next = nil return newHead } func printList(head *Node) { for head != nil { fmt.Printf("%d -> ", head.val) head = head.next } fmt.Println("null") } func main() { head := &Node{val: 42} head = reverseRecursive(head) printList(head) }
Attempts:
2 left
💡 Hint
A single node reversed is itself.
✗ Incorrect
The base case returns the node itself when next is nil, so the list remains unchanged.
🔧 Debug
advancedIdentify the Error in Recursive Reverse Implementation
What error will this recursive reverse function cause when run on a list with multiple nodes?
DSA Go
func reverseRecursive(head *Node) *Node {
if head == nil {
return head
}
newHead := reverseRecursive(head.next)
head.next.next = head
head.next = nil
return newHead
}Attempts:
2 left
💡 Hint
Check the base case for when head.next is nil.
✗ Incorrect
The base case does not check if head.next is nil, so head.next.next causes nil pointer dereference when head.next is nil.
❓ Predict Output
advancedOutput After Recursive Reverse on a 4-Node List
What is the printed state of the linked list after recursively reversing this 4-node list?
DSA Go
package main import "fmt" type Node struct { val int next *Node } func reverseRecursive(head *Node) *Node { if head == nil || head.next == nil { return head } newHead := reverseRecursive(head.next) head.next.next = head head.next = nil return newHead } func printList(head *Node) { for head != nil { fmt.Printf("%d -> ", head.val) head = head.next } fmt.Println("null") } func main() { head := &Node{val: 10} head.next = &Node{val: 20} head.next.next = &Node{val: 30} head.next.next.next = &Node{val: 40} head = reverseRecursive(head) printList(head) }
Attempts:
2 left
💡 Hint
The recursive function reverses the entire list.
✗ Incorrect
The recursive reversal flips the links so the last node becomes the head and the order is reversed.
🧠 Conceptual
expertWhy Does Recursive Reverse Set head.next to nil?
In the recursive reverse function, why do we set 'head.next = nil' after reversing the rest of the list?
Attempts:
2 left
💡 Hint
Think about what happens if we don't break the original link.
✗ Incorrect
Setting 'head.next = nil' breaks the original link so the reversed list does not form a cycle and ends properly.
