Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the doubly linked list is empty before deletion.
DSA C++
if (head == [1]) { return; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or NULL instead of nullptr.
Comparing head to itself.
✗ Incorrect
The head pointer is compared to nullptr to check if the list is empty.
2fill in blank
mediumComplete the code to move to the last node in the doubly linked list.
DSA C++
Node* temp = head; while (temp->[1] != nullptr) { temp = temp->next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using prev instead of next to traverse forward.
Checking data instead of next pointer.
✗ Incorrect
We traverse the list by moving to the next node until next is nullptr.
3fill in blank
hardFix the error in the code that deletes the last node from the doubly linked list.
DSA C++
if (temp->prev != nullptr) { temp->prev->[1] = nullptr; } free(temp);
Attempts:
3 left
💡 Hint
Common Mistakes
Setting prev->prev to nullptr instead of prev->next.
Not updating pointers before freeing the node.
✗ Incorrect
To remove the last node, set the previous node's next pointer to nullptr.
4fill in blank
hardFill both blanks to correctly update the head pointer when the list has only one node.
DSA C++
if (head->[1] == nullptr && head->[2] == nullptr) { free(head); head = nullptr; }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking only one pointer.
Not setting head to nullptr after deletion.
✗ Incorrect
If both next and prev are nullptr, the list has one node which is deleted and head set to nullptr.
5fill in blank
hardFill all three blanks to complete the function that deletes the last node from a doubly linked list.
DSA C++
void deleteEnd(Node*& head) {
if (head == [1]) return;
Node* temp = head;
while (temp->[2] != nullptr) {
temp = temp->next;
}
if (temp->prev != nullptr) {
temp->prev->[3] = nullptr;
} else {
head = nullptr;
}
free(temp);
}Attempts:
3 left
💡 Hint
Common Mistakes
Not checking if list is empty before deletion.
Using prev pointer to traverse forward.
Not updating the previous node's next pointer.
✗ Incorrect
Check if head is nullptr, traverse using next, and update prev node's next pointer to nullptr.
