Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the list is empty before deletion.
DSA C++
if (head == [1]) { return head; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or NULL instead of nullptr.
Checking head against head itself.
✗ Incorrect
We check if head is nullptr to see if the list is empty.
2fill in blank
mediumComplete the code to delete the head node if it matches the value.
DSA C++
if (head->data == [1]) { Node* temp = head; head = head->next; delete temp; return head; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'data' instead of 'value'.
Comparing with node pointer instead of data.
✗ Incorrect
We compare head->data with the value to delete, which is named 'value'.
3fill in blank
hardFix the error in the while loop condition to traverse the list correctly.
DSA C++
while (current != [1] && current->data != value) { prev = current; current = current->next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of nullptr.
Checking current against head or current itself.
✗ Incorrect
We check current against nullptr to avoid dereferencing a null pointer.
4fill in blank
hardFill both blanks to correctly unlink and delete the target node.
DSA C++
if (current != nullptr) { [1]->next = current->[2]; delete current; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using current->next instead of prev->next.
Mixing up prev and next pointers.
✗ Incorrect
We set prev->next to current->next to unlink the node before deleting it.
5fill in blank
hardFill all three blanks to complete the function that deletes a node by value.
DSA C++
Node* deleteNode(Node* head, int value) {
if (head == [1]) return head;
if (head->data == [2]) {
Node* temp = head;
head = head->next;
delete temp;
return head;
}
Node* prev = head;
Node* current = head->next;
while (current != [3] && current->data != value) {
prev = current;
current = current->next;
}
if (current != nullptr) {
prev->next = current->next;
delete current;
}
return head;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of nullptr.
Comparing with wrong variable names.
Incorrect loop condition.
✗ Incorrect
We check head against nullptr, compare data with value, and check current against nullptr in the loop.
