Complete the code to check if the list is empty before deletion.
if (this.head === [1]) { return; }
We check if this.head is null to know if the list is empty.
Complete the code to move to the next node while searching for the value.
while (current !== null && current.data !== value) { current = current[1]; }
We move to the next node to continue searching forward.
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 { this.[2] = current.prev; }
The next node's prev pointer is updated to skip the deleted node. If current.next is null, it means we deleted the tail, so update this.tail.
Fill all three blanks to complete the deleteByValue method in the doubly linked list.
deleteByValue(value: number): void {
if (this.head === [1]) {
return;
}
let current = this.head;
while (current !== null && current.data !== value) {
current = current.[2];
}
if (current === null) {
return;
}
if (current.prev !== null) {
current.prev.next = current.next;
} else {
this.head = current.next;
}
if (current.next !== null) {
current.next.[3] = current.prev;
} else {
this.tail = current.prev;
}
}The method first checks if the list is empty by comparing this.head to null. Then it moves through the list using next pointers to find the node with the matching value. Finally, it updates the prev pointer of the next node to remove the current node from the list.
