Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a dummy node pointing to the head.
DSA Rust
let mut dummy = ListNode { val: 0, next: Some(Box::new([1])) };Attempts:
3 left
💡 Hint
Common Mistakes
Using None instead of head causes the list to be empty.
Using dummy or tail is incorrect because they are not the head.
✗ Incorrect
We create a dummy node whose next points to the head of the list to simplify edge cases.
2fill in blank
mediumComplete the code to move the fast pointer n steps ahead.
DSA Rust
for _ in 0..[1] { fast = fast.unwrap().next.as_ref(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Moving fast n+1 steps causes off-by-one errors.
Moving fast n-1 steps causes incorrect gap.
✗ Incorrect
We move the fast pointer exactly n steps ahead to maintain the gap.
3fill in blank
hardFix the error in the code to correctly remove the nth node from the end.
DSA Rust
slow.as_mut().unwrap().next = slow.as_mut().unwrap().next.as_mut().unwrap().[1].take();Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' or 'val' causes compilation errors.
Using 'head' is not a field of the node.
✗ Incorrect
To remove the node, we link slow's next to the node after the next node, which is slow.next.next.
4fill in blank
hardFill both blanks to move fast and slow pointers until fast reaches the end.
DSA Rust
while fast.is_some() { fast = fast.unwrap().next.as_ref(); slow = slow.[1]().unwrap(); slow = slow.[2](); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using as_ref() gives immutable reference, so we cannot modify.
Using take() removes the value, which is not intended here.
✗ Incorrect
We use as_mut() to get a mutable reference and unwrap() to access the inner value for slow pointer movement.
5fill in blank
hardFill all three blanks to return the modified list after removal.
DSA Rust
return dummy.[1].[2](); // Where dummy is ListNode and next is Option<Box<ListNode>>
Attempts:
3 left
💡 Hint
Common Mistakes
Using as_ref() returns a reference, not ownership.
Not using take() causes borrowing errors.
✗ Incorrect
We return dummy.next.take().unwrap() to get ownership of the new head after removal.
