Complete the code to update the head pointer when deleting the first node in a linked list.
self.head = [1];To delete the first node, we update the head to point to the next node. Using self.head.as_mut().and_then(|node| node.next.take()) moves ownership of the next node to head.
Complete the code to check if the linked list is empty before deleting the first node.
if self.head.is_[1]() { return None; }
is_empty() which is not a method on Option in this context.is_some() which checks for presence, not absence.none() or nil().We use is_none() to check if the head is None, meaning the list is empty.
Fix the error in the code to properly return the value of the deleted node.
let old_head = self.head.take(); if let Some(node) = old_head { self.head = node.next; return Some(node.[1]); } else { return None; }
val or item which do not exist.The node's stored value is accessed by the field value in this linked list implementation.
Fill both blanks to safely update the head and return the deleted node's value.
let old_head = self.head.[1](); if let Some(node) = old_head { self.head = node.[2]; Some(node.value) } else { None }
clone() which copies instead of moves ownership.unwrap() which panics if None.We use take() to move the head out safely, then assign node.next to head to delete the first node.
Fill all three blanks to implement the full delete at beginning function returning the deleted value.
pub fn delete_at_beginning(&mut self) -> Option<T> {
let old_head = self.head.[1]();
if let Some(node) = old_head {
self.head = node.[2];
return Some(node.[3]);
}
None
}clone() instead of take().This function moves the head out with take(), updates head to the next node, and returns the deleted node's value.
