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 1->2->3->null
What is the printed state of the linked list after recursively reversing the list 1 -> 2 -> 3 -> null?
DSA Rust
struct Node {
val: i32,
next: Option<Box<Node>>,
}
fn reverse_recursive(head: Option<Box<Node>>) -> Option<Box<Node>> {
match head {
None => None,
Some(mut node) => {
if node.next.is_none() {
Some(node)
} else {
let next = node.next.take();
let new_head = reverse_recursive(next);
let mut tail = new_head.as_ref();
while let Some(t) = tail {
if t.next.is_none() {
break;
}
tail = t.next.as_ref();
}
if let Some(t) = tail {
let raw_tail = unsafe { &mut *(t as *const _ as *mut _) };
raw_tail.next = Some(node);
}
node.next = None;
new_head
}
}
}
}
fn print_list(head: &Option<Box<Node>>) {
let mut current = head.as_ref();
while let Some(node) = current {
print!("{} -> ", node.val);
current = node.next.as_ref();
}
println!("null");
}
fn main() {
let node3 = Box::new(Node { val: 3, next: None });
let node2 = Box::new(Node { val: 2, next: Some(node3) });
let node1 = Box::new(Node { val: 1, next: Some(node2) });
let head = Some(node1);
let reversed = reverse_recursive(head);
print_list(&reversed);
}Attempts:
2 left
💡 Hint
Think about how recursion reaches the last node and then reverses the links on the way back.
✗ Incorrect
The recursive function goes to the end of the list, then reverses the links one by one as it returns back. So the list 1->2->3->null becomes 3->2->1->null.
❓ Predict Output
intermediateOutput of Recursive Reverse on Empty List
What is the printed state of the linked list after recursively reversing an empty list (null)?
DSA Rust
fn main() {
let head: Option<Box<Node>> = None;
let reversed = reverse_recursive(head);
print_list(&reversed);
}Attempts:
2 left
💡 Hint
What happens when the input list is empty?
✗ Incorrect
If the list is empty, the function returns None and printing shows 'null'.
🔧 Debug
advancedIdentify the Bug in Recursive Reverse Implementation
Which option shows the bug that causes infinite recursion or stack overflow in this recursive reverse function?
DSA Rust
fn reverse_recursive(head: Option<Box<Node>>) -> Option<Box<Node>> {
match head {
None => None,
Some(mut node) => {
if node.next.is_none() {
Some(node)
} else {
let next = node.next.take();
let new_head = reverse_recursive(next);
node.next.as_mut().unwrap().next = Some(node);
node.next = None;
new_head
}
}
}
}Attempts:
2 left
💡 Hint
Check how the pointers are reassigned after recursion call.
✗ Incorrect
Assigning node.next.as_mut().unwrap().next = Some(node) creates a cycle by pointing a node to itself, causing infinite recursion or stack overflow.
🧠 Conceptual
advancedWhy Use Recursion to Reverse a Linked List?
Which is the main advantage of using recursion to reverse a singly linked list compared to iteration?
Attempts:
2 left
💡 Hint
Think about how recursion processes nodes from the end back to the start.
✗ Incorrect
Recursion processes the list from the last node back to the first, which matches the reversal process and often leads to simpler code.
❓ Predict Output
expertOutput After Recursive Reverse on 1->2->3->4->null with Print Statements
Given the recursive reverse function below with added print statements, what is the exact output when reversing the list 1 -> 2 -> 3 -> 4 -> null?
DSA Rust
struct Node {
val: i32,
next: Option<Box<Node>>,
}
fn reverse_recursive(head: Option<Box<Node>>) -> Option<Box<Node>> {
match head {
None => {
println!("Reached end: None");
None
},
Some(mut node) => {
println!("Visiting node: {}", node.val);
if node.next.is_none() {
println!("Base case reached at node: {}", node.val);
Some(node)
} else {
let next = node.next.take();
let new_head = reverse_recursive(next);
let mut tail = new_head.as_ref();
while let Some(t) = tail {
if t.next.is_none() {
break;
}
tail = t.next.as_ref();
}
if let Some(t) = tail {
let raw_tail = unsafe { &mut *(t as *const _ as *mut _) };
raw_tail.next = Some(node);
}
node.next = None;
new_head
}
}
}
}
fn print_list(head: &Option<Box<Node>>) {
let mut current = head.as_ref();
while let Some(node) = current {
print!("{} -> ", node.val);
current = node.next.as_ref();
}
println!("null");
}
fn main() {
let node4 = Box::new(Node { val: 4, next: None });
let node3 = Box::new(Node { val: 3, next: Some(node4) });
let node2 = Box::new(Node { val: 2, next: Some(node3) });
let node1 = Box::new(Node { val: 1, next: Some(node2) });
let head = Some(node1);
let reversed = reverse_recursive(head);
print_list(&reversed);
}Attempts:
2 left
💡 Hint
Trace the recursive calls from head to tail and back.
✗ Incorrect
The function visits nodes from 1 to 4, then hits the base case at 4, then reverses the list on the way back, printing the reversed list.
