Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the list is empty before deletion.
DSA Rust
if self.head.is_[1]() { return; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using is_empty() which does not exist for Option.
Using is_some() which checks for presence, not absence.
✗ Incorrect
In Rust, Option types use is_none() to check if the value is None (empty).
2fill in blank
mediumComplete the code to move to the next node in the list.
DSA Rust
while let Some(ref mut current) = current_node { if current.value == value { break; } current_node = current.[1].as_mut(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is for doubly linked lists.
Using 'child' or 'link' which are not standard field names.
✗ Incorrect
In a singly linked list, the next node is accessed via the 'next' field.
3fill in blank
hardFix the error in updating the next pointer to delete the node.
DSA Rust
if let Some(ref mut prev_node) = prev { prev_node.next = current_node.[1].take(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using prev instead of next causes wrong pointer update.
Not using take() leads to ownership errors.
✗ Incorrect
To delete a node, link the previous node's next to the current node's next.
4fill in blank
hardFill both blanks to correctly handle deleting the head node.
DSA Rust
if self.head.as_ref().map_or(false, |node| node.value == value) { self.head = self.head.as_mut().and_then(|node| node.[1].[2]()); return; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using clone() which duplicates nodes unnecessarily.
Using unwrap() which can panic if next is None.
✗ Incorrect
To delete the head, set head to the next node by taking ownership of it.
5fill in blank
hardFill all three blanks to complete the delete function safely.
DSA Rust
pub fn delete(&mut self, value: i32) {
if self.head.is_none() {
return;
}
if self.head.as_ref().unwrap().value == [1] {
self.head = self.head.as_mut().and_then(|node| node.[2].[3]());
return;
}
let mut current_node = self.head.as_mut();
while let Some(ref mut current) = current_node {
if let Some(ref mut next_node) = current.[2] {
if next_node.value == value {
current.[2] = next_node.[2].take();
return;
}
}
current_node = current.[2].as_mut();
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' in a singly linked list.
Not using take() causing ownership errors.
✗ Incorrect
The function compares the value to delete, uses 'next' to traverse, and 'take()' to remove nodes safely.
