Complete the code to check if the linked list is empty before deletion.
if (this.head === [1]) { return null; }
We check if this.head is null to know if the list is empty.
Complete the code to traverse the list to find the second last node.
while (current.next && current.next.next) { current = [1]; }
We move current to current.next to reach the second last node.
Fix the error in the code that deletes the last node by updating the tail.
current.next = null;
this.tail = [1];The tail should point to the current node after deleting the last node.
Fill both blanks to handle the case when the list has only one node.
if (this.head === this.tail) { this.head = [1]; this.tail = [2]; return; }
When only one node exists, deleting it sets both head and tail to null.
Fill all three blanks to complete the deleteNodeAtEnd method.
deleteNodeAtEnd() {
if (this.head === [1]) return null;
if (this.head === this.tail) {
this.head = [2];
this.tail = [3];
return;
}
let current = this.head;
while (current.next && current.next.next) {
current = current.next;
}
current.next = null;
this.tail = current;
}The method checks if the list is empty (head is null), then if only one node exists (head equals tail), sets both to null, else finds the second last node and updates tail.
