Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA Rust
let new_node = Box::new(Node { data: [1], next: None });Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of the value for the data field.
Using 'head' which is a pointer, not the value.
✗ Incorrect
We create a new node with the data field set to the given value.
2fill in blank
mediumComplete the code to check if the position to delete is the head (position 0).
DSA Rust
if position == [1] {
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for position == 1 instead of 0.
Using None which is not a number.
✗ Incorrect
Position 0 means the head node should be deleted.
3fill in blank
hardFix the error in the loop condition to traverse to the node before the one to delete.
DSA Rust
for _ in 0..([1] - 1) { current = current.next.as_mut().unwrap(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using position + 1 causes out-of-bounds traversal.
Using position - 1 inside the range causes incorrect loop count.
✗ Incorrect
We loop from 0 to position - 1 to reach the node before the target.
4fill in blank
hardFill both blanks to correctly update the next pointer to skip the deleted node.
DSA Rust
let next_node = current.next.as_mut().unwrap().next.take();
current.next = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning current.next to None removes all nodes after current.
Assigning current.next to current.next causes no change.
✗ Incorrect
We assign current.next to next_node to remove the target node from the list.
5fill in blank
hardFill all three blanks to complete the function that deletes a node at a given position.
DSA Rust
pub fn delete_at_position(mut head: Option<Box<Node>>, position: usize) -> Option<Box<Node>> {
if position == [1] {
return head.and_then(|node| node.next);
}
let mut current = head.as_mut().unwrap();
for _ in 0..([2] - 1) {
current = current.next.as_mut().unwrap();
}
let next_node = current.next.as_mut().unwrap().next.take();
current.next = [3];
head
}Attempts:
3 left
💡 Hint
Common Mistakes
Not handling deletion of head node separately.
Incorrect loop range causing panic.
Assigning current.next incorrectly.
✗ Incorrect
Position 0 means delete head; loop to position - 1; update current.next to skip deleted node.
