Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define the base case for the recursive reverse function.
DSA Rust
fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if head.is_none() || head.as_ref().unwrap().next.is_none() {
return [1];
}
// recursive case omitted
None
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning None instead of head for base case
Trying to unwrap head without checking
✗ Incorrect
The base case returns the head itself when the list is empty or has one node, so we return a clone of head.
2fill in blank
mediumComplete the code to recursively reverse the rest of the list after the head.
DSA Rust
fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if head.is_none() || head.as_ref().unwrap().next.is_none() {
return head.clone();
}
let mut head = head.unwrap();
let rest = head.next.take();
let reversed = [1];
// rest of code omitted
None
}Attempts:
3 left
💡 Hint
Common Mistakes
Calling reverse_list on head.next without taking ownership
Passing None instead of rest
✗ Incorrect
We recursively call reverse_list on rest, which is the next part of the list after head.
3fill in blank
hardFix the error in linking the reversed list back to the current head node.
DSA Rust
fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if head.is_none() || head.as_ref().unwrap().next.is_none() {
return head.clone();
}
let mut head = head.unwrap();
let rest = head.next.take();
let reversed = reverse_list(rest);
[1];
head.next = None;
reversed
}Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access head.next when it is None
Assigning to head.next instead of reversed list
✗ Incorrect
We must set the next of the last node in reversed list to head, so we use reversed.as_mut().unwrap().next = Some(head).
4fill in blank
hardFill both blanks to correctly complete the recursive reverse function.
DSA Rust
fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if head.is_none() || head.as_ref().unwrap().next.is_none() {
return [1];
}
let mut head = head.unwrap();
let rest = head.next.take();
let reversed = reverse_list(rest);
[2];
head.next = None;
reversed
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning None in base case
Incorrectly linking nodes in recursive step
✗ Incorrect
Return head.clone() in base case and link reversed list's last node to head with reversed.as_mut().unwrap().next = Some(head).
5fill in blank
hardFill all three blanks to implement the recursive reverse function correctly.
DSA Rust
fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
if [1] || head.as_ref().unwrap().next.is_none() {
return head.clone();
}
let mut head = head.unwrap();
let rest = head.next.take();
let reversed = reverse_list(rest);
[2];
head.next = [3];
reversed
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head.is_some() in base case condition
Forgetting to set head.next to None
✗ Incorrect
Check if head is None for base case, link reversed list's last node to head, and set head.next to None to avoid cycles.
