Complete the code to check if the list is empty before deletion.
if (head == [1]) { return; }
We check if head is null to know if the list is empty.
Complete the code to find the node with the given value.
Node current = head; while (current != null && current.data != [1]) { current = current.next; }
We compare each node's data with the value to find the node to delete.
Fix the error in updating the previous node's next pointer after deletion.
if (current.prev != null) { current.prev.[1] = current.next; }
We update the next pointer of the previous node to skip the deleted node.
Fill both blanks to update the next node's previous pointer and handle head update.
if (current.next != null) { current.next.[1] = current.prev; } else { head = [2]; }
The next node's prev pointer should point to the previous node. If the deleted node was the last, update head to the previous node.
Fill all three blanks to complete the delete by value method in doubly linked list.
public void DeleteByValue(int value) {
if (head == [1]) return;
Node current = head;
while (current != null && current.data != value) {
current = current.next;
}
if (current == null) return;
if (current.prev != null) {
current.prev.[2] = current.next;
}
if (current.next != null) {
current.next.[3] = current.prev;
} else {
head = current.prev;
}
}We check if the list is empty by comparing head to null. Then update the previous node's next pointer and the next node's prev pointer to remove the current node.
