Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return true if the value is found in the linked list.
DSA C++
bool search(Node* head, int target) {
Node* current = head;
while (current != nullptr) {
if (current->data == [1]) {
return true;
}
current = current->next;
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing current->data with head instead of target.
Using nullptr in the comparison.
Comparing with current pointer instead of data.
✗ Incorrect
The function compares each node's data with the target value to find a match.
2fill in blank
mediumComplete the code to move to the next node in the linked list.
DSA C++
bool search(Node* head, int target) {
Node* current = head;
while (current != nullptr) {
if (current->data == target) {
return true;
}
current = [1];
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head->next instead of current->next.
Using target->next which is invalid.
Assigning nullptr which stops traversal.
✗ Incorrect
To traverse the list, move current to current->next each time.
3fill in blank
hardFix the error in the condition to check if the list is empty.
DSA C++
bool search(Node* head, int target) {
if (head == [1]) {
return false;
}
Node* current = head;
while (current != nullptr) {
if (current->data == target) {
return true;
}
current = current->next;
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of nullptr.
Comparing head with itself.
Comparing head with target value.
✗ Incorrect
An empty list is indicated by head being nullptr, not 0 or other values.
4fill in blank
hardFill both blanks to create a function that returns the node containing the target value or nullptr if not found.
DSA C++
Node* searchNode(Node* head, int target) {
Node* current = head;
while (current != [1]) {
if (current->data == [2]) {
return current;
}
current = current->next;
}
return nullptr;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head instead of nullptr in the loop condition.
Comparing current->data with head or current instead of target.
✗ Incorrect
The loop continues until current is nullptr, and compares current->data with target.
5fill in blank
hardFill all three blanks to create a function that counts how many times the target value appears in the linked list.
DSA C++
int countOccurrences(Node* head, int target) {
int count = 0;
Node* current = [1];
while (current != [2]) {
if (current->data == [3]) {
count++;
}
current = current->next;
}
return count;
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting from nullptr instead of head.
Looping until head instead of nullptr.
Comparing data with count variable.
✗ Incorrect
Start from head, loop until current is nullptr, and compare current->data with target to count matches.
