Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the linked list contains the target value.
DSA Java
while (current != null) { if (current.data [1] target) { return true; } current = current.next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes the search to fail.
Using '>' or '<' compares values incorrectly for equality.
✗ Incorrect
We use '==' to check if the current node's data equals the target value.
2fill in blank
mediumComplete the code to move to the next node in the linked list.
DSA Java
current = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning current to head causes an infinite loop.
Assigning current to target or null stops traversal prematurely.
✗ Incorrect
To move forward in the list, assign current to current.next.
3fill in blank
hardFix the error in the search method to return false if the value is not found.
DSA Java
public boolean search(int target) {
Node current = head;
while (current != null) {
if (current.data == target) {
return true;
}
current = current.next;
}
return [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning true always causes incorrect results.
Returning null or current causes compile errors.
✗ Incorrect
If the loop ends without finding the target, return false to indicate not found.
4fill in blank
hardFill both blanks to create a method that searches and returns the node containing the target value or null if not found.
DSA Java
public Node findNode(int target) {
Node current = [1];
while (current != null) {
if (current.data == target) {
return [2];
}
current = current.next;
}
return null;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning target instead of the node causes type errors.
Starting from null causes no traversal.
✗ Incorrect
Start from head and return the current node when the target is found.
5fill in blank
hardFill all three blanks to create a method that returns true if the linked list contains the target value, false otherwise.
DSA Java
public boolean contains(int target) {
Node current = [1];
while ([2] != null) {
if (current.data [3] target) {
return true;
}
current = current.next;
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' in comparison.
Looping with head instead of current causes infinite loop.
✗ Incorrect
Start from head, loop while current is not null, and check if current.data equals target.
