Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the linked list is empty before deletion.
DSA Java
if (head == [1]) { return null; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if head equals tail instead of null.
Using 'next' instead of null.
✗ Incorrect
We check if head is null to know if the list is empty before deleting.
2fill in blank
mediumComplete the code to handle the case when the list has only one node.
DSA Java
if (head.next == [1]) { return null; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if head.next equals head.
Using tail instead of null.
✗ Incorrect
If head.next is null, it means only one node exists, so deleting it returns null.
3fill in blank
hardFix the error in the loop condition to find the second last node.
DSA Java
while (current.next.[1] != null) { current = current.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using current.next instead of current.next.next.
Using prev which is not defined in singly linked list.
✗ Incorrect
We check current.next.next != null to stop at second last node before last node.
4fill in blank
hardFill both blanks to correctly remove the last node.
DSA Java
Node current = head; while (current.next.[1] != null) { current = current.next; } current.next = [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Setting current.next to head instead of null.
Using prev which is not defined.
✗ Incorrect
We loop while current.next.next != null to reach second last node, then set current.next to null to delete last node.
5fill in blank
hardFill all three blanks to complete the delete last node method.
DSA Java
public Node deleteLast(Node head) {
if (head == [1]) {
return null;
}
if (head.next == [2]) {
return null;
}
Node current = head;
while (current.next.[3] != null) {
current = current.next;
}
current.next = null;
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head instead of null for empty check.
Using tail which is undefined.
Loop condition using current.next instead of current.next.next.
✗ Incorrect
Check if head is null or head.next is null to handle empty or single node list. Loop while current.next.next != null to find second last node.
