Complete the code to check if the position is valid before deletion.
if (position < 0 || position >= this.length) { return [1]; }
Returning null indicates the position is invalid and no deletion occurs.
Complete the code to delete the head node when position is 0.
if (position === 0) { this.head = this.head.[1]; this.length--; return; }
prev instead of next.To delete the head node, update this.head to point to the next node.
Fix the error in updating the previous node's next pointer during deletion.
let current = this.head; let previous = null; let index = 0; while (index < position) { previous = current; current = current.[1]; index++; } previous.next = current.next; this.length--;
prev which is undefined.previous.next correctly.We move forward in the list using the next pointer to reach the node to delete.
Fill both blanks to complete the loop that finds the node before the one to delete.
let current = this.head; let index = 0; while (index < position - 1) { current = current.[1]; index[2]; }
prev which does not exist.We move forward using next and increment the index with ++ to reach the node before the target.
Fill all three blanks to complete the delete function that removes a node at a given position.
deleteAtPosition(position: number): void {
if (position < 0 || position >= this.length) {
return null;
}
if (position === 0) {
this.head = this.head.[1];
} else {
let current = this.head;
let index = 0;
while (index < position - 1) {
current = current.[2];
index[3];
}
current.next = current.next.next;
}
this.length--;
}prev which does not exist.To delete a node, update head to head.next if deleting the first node. Otherwise, traverse using next and increment the index with ++.
