Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the head node contains the value to delete.
DSA Javascript
if (this.head && this.head.value === [1]) { this.head = this.head.next; return; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined like 'val' or 'target'.
Comparing to 'node' which is an object, not the value.
✗ Incorrect
The value to delete is stored in the variable 'value', so we compare head.value to 'value'.
2fill in blank
mediumComplete the code to traverse the list and find the node before the one to delete.
DSA Javascript
let current = this.head; while (current.next && current.next.value !== [1]) { current = current.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'node' or 'target' which are not the parameter name.
Comparing current.value instead of current.next.value.
✗ Incorrect
We compare current.next.value to the 'value' parameter to find the node to delete.
3fill in blank
hardFix the error in the code to delete the node by updating the links correctly.
DSA Javascript
if (current.next) { current.next = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting current.next to current.next (no change).
Setting current.next to current (creates a cycle).
✗ Incorrect
To delete the node, we skip it by pointing current.next to current.next.next.
4fill in blank
hardFill both blanks to complete the delete function that removes the first node with the given value.
DSA Javascript
delete(value) {
if (this.head && this.head.value === [1]) {
this.head = this.head.next;
return;
}
let current = this.head;
while (current.next && current.next.value !== [2]) {
current = current.next;
}
if (current.next) {
current.next = current.next.next;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the two comparisons.
Forgetting to check the head node separately.
✗ Incorrect
Both blanks compare node values to the 'value' parameter to find and delete the node.
5fill in blank
hardFill all three blanks to implement a delete method that removes the first node with the given value from the linked list.
DSA Javascript
delete(value) {
if (this.head && this.head.value === [1]) {
this.head = this.head.next;
return;
}
let current = this.head;
while (current.next && current.next.value !== [2]) {
current = current.next;
}
if (current.next) {
current.next = [3];
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for comparison.
Setting current.next incorrectly, causing broken links.
✗ Incorrect
The first two blanks compare node values to 'value'. The third blank skips the node by linking to current.next.next.
