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; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or NULL instead of nullptr.
Checking if head equals head itself.
✗ Incorrect
We check if head is nullptr to know if the list is empty.
2fill in blank
mediumComplete the code to find the node with the given value.
DSA C++
Node* current = head; while (current != nullptr && current->data != [1]) { current = current->next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' instead of the variable holding the value.
Using 'node' which is undefined.
✗ Incorrect
We compare current->data with the value to find the node to delete.
3fill in blank
hardFix the error in updating the previous node's next pointer after deletion.
DSA C++
if (current->prev != nullptr) { current->prev->[1] = current->next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Updating prev instead of next.
Trying to update data or value which are not pointers.
✗ Incorrect
We update the previous node's next pointer to skip the deleted node.
4fill in blank
hardFill both blanks to update the next node's previous pointer and handle head update.
DSA C++
if (current->next != nullptr) { current->next->[1] = current->prev; } if (current == [2]) { head = current->next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using next instead of prev for the first blank.
Comparing current to current instead of head.
✗ Incorrect
We update the next node's prev pointer and if current is head, update head.
5fill in blank
hardFill all three blanks to complete the delete by value function in doubly linked list.
DSA C++
void deleteByValue(Node*& head, int value) {
if (head == [1]) return;
Node* current = head;
while (current != nullptr && current->data != value) {
current = current->next;
}
if (current == nullptr) return;
if (current->prev != nullptr) {
current->prev->next = current->next;
}
if (current->next != nullptr) {
current->next->[2] = current->prev;
}
if (current == [3]) {
head = current->next;
}
delete current;
}Attempts:
3 left
💡 Hint
Common Mistakes
Not checking if list is empty.
Wrong pointer updated for next node's prev.
Not updating head when deleting the first node.
✗ Incorrect
We check if head is nullptr, update next node's prev pointer, and update head if needed.
