Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the list is empty before deletion.
DSA Java
if (head == [1]) { return null; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' instead of 'null' causes infinite loops.
Checking 'tail' instead of 'head' misses empty list cases.
✗ Incorrect
We check if head is null to know if the list is empty.
2fill in blank
mediumComplete the code to delete the only node when head points to itself.
DSA Java
if (head.next == [1]) { head = null; return head; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'null' instead of 'head' causes wrong condition.
Not handling single node case leads to errors.
✗ Incorrect
If head.next is head, it means only one node exists.
3fill in blank
hardFix the error in the loop condition to find the node before the one to delete.
DSA Java
Node current = head;
do {
if (current.next.data == key) {
break;
}
current = current.[1];
} while (current != head);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' causes errors because it's not defined.
Using 'data' instead of 'next' breaks traversal.
✗ Incorrect
We move to the next node by using current.next.
4fill in blank
hardFill the blank to update links and delete the target node.
DSA Java
if (current.next == head) { head = head.next; } current.[1] = current.next.next;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' causes errors because it's not defined.
Not updating head when deleting the first node causes bugs.
✗ Incorrect
We set current.next to skip the deleted node.
5fill in blank
hardFill all three blanks to complete the deleteNode method for circular linked list.
DSA Java
public Node deleteNode(Node head, int key) {
if (head == [1]) return null;
if (head.next == head && head.data == key) {
return null;
}
Node current = head;
while (current.next != head && current.next.data != key) {
current = current.[2];
}
if (current.next.data == key) {
if (current.next == head) {
head = head.[3];
}
current.next = current.next.next;
}
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is not defined.
Not updating head when deleting the first node.
✗ Incorrect
We check if head is null, move current to next, and update head to head.next when deleting first node.
