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 Java
if (head != null && head.data [1] value) { head = head.next; return; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes the wrong node to be deleted.
Using comparison operators like '>' or '<' is incorrect here.
✗ Incorrect
We check if the head node's data is equal to the value to delete. If yes, we remove the head by pointing it to the next node.
2fill in blank
mediumComplete the while loop condition to traverse the list until the next node is null or contains the value.
DSA Java
Node current = head; while (current.next != null && current.next.data [1] value) { current = current.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' causes the loop to stop too early.
Using '>' or '<' is not suitable for equality checks.
✗ Incorrect
We continue moving forward while the next node's data is not equal to the value. This stops at the node before the one to delete.
3fill in blank
hardFix the error in deleting the node by correctly updating the next pointer.
DSA Java
if (current.next != null) { current.next = current.next.[1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' or 'data' causes errors because those fields don't exist or are not pointers.
Using 'head' here is incorrect as it refers to the start of the list.
✗ Incorrect
To delete the node, we skip it by pointing current.next to the node after the one to delete, which is current.next.next.
4fill in blank
hardFill both blanks to complete the method that deletes a node by value from the linked list.
DSA Java
public void deleteByValue(int value) {
if (head == null) return;
if (head.data [1] value) {
head = head.[2];
return;
}
Node current = head;
while (current.next != null && current.next.data != value) {
current = current.next;
}
if (current.next != null) {
current.next = current.next.next;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' in the first blank causes wrong deletion logic.
Using 'prev' instead of 'next' for head update causes errors.
✗ Incorrect
We check if head's data equals the value, then update head to head.next to delete it.
5fill in blank
hardFill all three blanks to create a method that deletes a node by value and returns true if deleted, false otherwise.
DSA Java
public boolean deleteNode(int value) {
if (head == null) return false;
if (head.data [1] value) {
head = head.[2];
return [3];
}
Node current = head;
while (current.next != null && current.next.data != value) {
current = current.next;
}
if (current.next != null) {
current.next = current.next.next;
return true;
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning false after deleting head is wrong.
Using '!=' instead of '==' in first blank breaks logic.
✗ Incorrect
We check if head's data equals value, update head to head.next, and return true to indicate deletion.
