Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the list is empty before deletion.
DSA Typescript
if (this.head === [1]) { return; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for undefined instead of null.
Using 0 or false which are not valid for empty list.
✗ Incorrect
The head of the list is null when the list is empty, so we check if this.head === null.
2fill in blank
mediumComplete the code to delete the head node if it matches the value.
DSA Typescript
if (this.head.value === [1]) { this.head = this.head.next; return; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name like value or node.
Comparing with this.value instead of val.
✗ Incorrect
We compare the head node's value with the given val to decide deletion.
3fill in blank
hardFix the error in the while loop condition to traverse the list correctly.
DSA Typescript
while (current.next !== [1]) { if (current.next.value === val) { current.next = current.next.next; return; } current = current.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined or false which do not mark list end.
Using 0 which is not a valid node reference.
✗ Incorrect
We check if current.next is not null to continue traversing the list.
4fill in blank
hardFill both blanks to correctly initialize and traverse the linked list for deletion.
DSA Typescript
let current = this.[1]; while (current.[2] !== null) { if (current.next.value === val) { current.next = current.next.next; return; } current = current.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from tail which is the end of the list.
Using prev which is for doubly linked lists.
✗ Incorrect
We start from the head and traverse using the next pointer to find the node to delete.
5fill in blank
hardFill all three blanks to complete the deleteNodeByValue method correctly.
DSA Typescript
deleteNodeByValue(val: number): void {
if (this.head === [1]) {
return;
}
if (this.head.value === val) {
this.head = this.head.[2];
return;
}
let current = this.head;
while (current.next !== [3]) {
if (current.next.value === val) {
current.next = current.next.next;
return;
}
current = current.next;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined instead of null.
Using head instead of next to update pointers.
✗ Incorrect
We check if head is null, update head to head.next if head matches val, and traverse while current.next is not null.
