Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a mutable reference to the head of the linked list.
DSA Rust
let mut current = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using immutable reference '&head' instead of mutable '&mut head'.
Using 'head' directly without reference.
✗ Incorrect
We need a mutable reference to the head to modify the list nodes.
2fill in blank
mediumComplete the code to find the middle node of the linked list using slow and fast pointers.
DSA Rust
while fast.is_some() && fast.as_ref().unwrap().next.is_some() { slow = slow.unwrap().next.as_ref(); fast = fast.unwrap().next.as_ref().unwrap().next.as_ref(); } let mid = [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using fast pointer as middle node.
Using head instead of slow pointer.
✗ Incorrect
The slow pointer ends at the middle node after the loop.
3fill in blank
hardFix the error in reversing the second half of the linked list.
DSA Rust
let mut prev = None; let mut curr = mid; while let Some(mut node) = curr { let next = node.next.take(); node.next = [1]; prev = Some(node); curr = next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning node.next to curr causing cycle.
Assigning node.next to next causing no reversal.
✗ Incorrect
We reverse by pointing current node's next to previous node.
4fill in blank
hardFill both blanks to merge the two halves of the list alternately.
DSA Rust
while second.is_some() { let mut temp1 = first.as_mut().unwrap().next.take(); let mut temp2 = second.as_mut().unwrap().next.take(); first.as_mut().unwrap().next = second; second = [1]; first = [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning second to first or vice versa causing infinite loop.
Not updating pointers correctly causing broken list.
✗ Incorrect
We move second to temp2 and first to temp1 to continue merging.
5fill in blank
hardFill all three blanks to complete the reorder list function in Rust.
DSA Rust
pub fn reorder_list(head: &mut Option<Box<ListNode>>) {
if head.is_none() || head.as_ref().unwrap().next.is_none() {
return;
}
let mut slow = head.as_ref();
let mut fast = head.as_ref();
while fast.is_some() && fast.as_ref().unwrap().next.is_some() {
slow = slow.unwrap().next.as_ref();
fast = fast.unwrap().next.as_ref().unwrap().next.as_ref();
}
let mut second = slow.unwrap().next.clone();
slow.unwrap().next = None;
let mut prev = None;
let mut curr = second;
while let Some(mut node) = curr {
let next = node.next.take();
node.next = [1];
prev = Some(node);
curr = next;
}
let mut first = head.take();
let mut second = prev;
while second.is_some() {
let mut temp1 = first.as_mut().unwrap().next.take();
let mut temp2 = second.as_mut().unwrap().next.take();
first.as_mut().unwrap().next = second;
second = [2];
first = [3];
}
*head = first;
}Attempts:
3 left
💡 Hint
Common Mistakes
Incorrect reversal causing cycles.
Wrong pointer updates causing broken merge.
✗ Incorrect
Reverse second half by pointing node.next to prev, then merge by updating second to temp2 and first to temp1.
