Complete the code to check if the linked list is empty before deletion.
if self.head.is_[1]() { return; }
is_some() with is_none().In Rust, Option types use is_none() to check if the value is empty (None).
Complete the code to handle the case when the list has only one node.
if self.head.as_ref().unwrap().next.is_[1]() { self.head = None; return; }
is_some() which checks presence, not absence.We check if the next pointer is None to know if there is only one node.
Fix the error in the loop condition to find the second last node.
while current.next.as_ref().unwrap().next.is_[1]() { current = current.next.as_mut().unwrap(); }
is_none() which stops too early.The loop continues while the next node's next exists (is_some()).
Fill both blanks to correctly remove the last node.
let last_node = current.next.take();
current.next = [1];
// last_node is dropped here
Some values which keep nodes linked.current.next to itself causing no change.Setting current.next to None removes the last node.
Fill all three blanks to complete the delete at end function.
pub fn delete_at_end(&mut self) {
if self.head.is_[1]() {
return;
}
if self.head.as_ref().unwrap().next.is_[2]() {
self.head = [3];
return;
}
let mut current = self.head.as_mut().unwrap();
while current.next.as_ref().unwrap().next.is_some() {
current = current.next.as_mut().unwrap();
}
current.next = None;
}is_some() instead of is_none() in checks.Some instead of None to head.The function checks if the list is empty (is_none()), then if only one node (next.is_none()), then sets head to None to delete it.
