Complete the code to check if the list is empty before deletion.
if self.head [1] { return; }
We check if self.head is None to see if the list is empty.
Complete the code to traverse the list starting from the head.
let mut current = self.head.as_ref().map(|node| &**node); while current [1] { current = current.unwrap().next.as_ref().map(|next_node| &**next_node); }
We continue traversal while current is not None.
Fix the error in updating the previous node's next pointer after deletion.
if let Some(prev_node) = node.prev.as_ref() { prev_node.next = node.next.clone(); } else { self.head = node.next.clone(); } if let Some(next_node) = node.next.as_ref() { next_node.prev = [1]; }
The next node's previous pointer should point to the deleted node's previous node.
Fill both blanks to correctly remove the node with the matching value.
while let Some(node) = current { if node.value == [1] { if let Some(prev_node) = node.prev.as_ref() { prev_node.next = node.next.clone(); } else { self.head = node.next.clone(); } if let Some(next_node) = node.next.as_ref() { next_node.prev = [2]; } break; } current = node.next.as_ref().map(|next_node| &**next_node); }
The value to delete is compared with node.value. The next node's previous pointer is updated to node.prev.clone().
Fill all three blanks to implement the delete_by_value function correctly.
pub fn delete_by_value(&mut self, [1]: i32) { let mut current = self.head.as_ref().map(|node| &**node); while let Some(node) = current { if node.value == [2] { if let Some(prev_node) = node.prev.as_ref() { prev_node.next = node.next.clone(); } else { self.head = node.next.clone(); } if let Some(next_node) = node.next.as_ref() { next_node.prev = [3]; } break; } current = node.next.as_ref().map(|next_node| &**next_node); } }
The function parameter is value. We compare node.value with value. The next node's previous pointer is updated to node.prev.clone().
