Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to delete the head node when position is 0.
DSA Javascript
if (position === 0) { head = head[1]; return head; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using .prev instead of .next
Trying to delete without updating head
Using incorrect property names
✗ Incorrect
To delete the head node, we move the head pointer to the next node using head.next.
2fill in blank
mediumComplete the code to traverse the list to the node before the one to delete.
DSA Javascript
let current = head; for (let i = 0; i < position - 1; i++) { current = current[1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using .prev which goes backward
Using incorrect property names
Not looping enough times
✗ Incorrect
To move forward in the list, we use the .next property to go to the next node.
3fill in blank
hardFix the error in the code to correctly delete the node at the given position.
DSA Javascript
if (current && current.next) { current.next = current.next[1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using .prev which is backward link
Using wrong property names
Not checking if current.next exists
✗ Incorrect
To delete the node, we link the current node's next to the node after the one being deleted using .next.
4fill in blank
hardFill both blanks to complete the function that deletes a node at a specific position.
DSA Javascript
function deleteNode(head, position) {
if (position === 0) {
head = head[1];
return head;
}
let current = head;
for (let i = 0; i < position - 1; i++) {
current = current[2];
}
if (current && current.next) {
current.next = current.next.next;
}
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using .prev or other properties instead of .next
Not updating head correctly when deleting the first node
✗ Incorrect
To delete the node, move head to head.next if position is 0, and traverse using .next to reach the node before the one to delete.
5fill in blank
hardFill all three blanks to complete the function that deletes a node at a specific position and returns the updated list.
DSA Javascript
function deleteNode(head, position) {
if (position === 0) {
head = head[1];
return head;
}
let current = head;
for (let i = 0; i < position - 1; i++) {
current = current[2];
}
if (current && current.next) {
current.next = current.next[3];
}
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using .prev or .child instead of .next
Not linking current.next to current.next.next correctly
✗ Incorrect
To delete the node, move head to head.next if position is 0, traverse using .next, and link current.next to current.next.next to skip the deleted node.
