Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to delete the first node from the doubly linked list.
DSA C++
void deleteBeginning(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->[1];
if (head != nullptr) {
head->prev = nullptr;
}
delete temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using head->prev instead of head->next.
Not updating head->prev to nullptr after deletion.
✗ Incorrect
To delete the first node, move the head pointer to the next node using head->next.
2fill in blank
mediumComplete the code to update the previous pointer of the new head after deletion.
DSA C++
void deleteBeginning(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->next;
if (head != nullptr) {
head->[1] = nullptr;
}
delete temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting head->next to nullptr instead of head->prev.
Not checking if head is nullptr before updating.
✗ Incorrect
After moving head to the next node, set head->prev to nullptr to mark it as the first node.
3fill in blank
hardFix the error in the code that deletes the first node of the doubly linked list.
DSA C++
void deleteBeginning(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->next;
if (head != nullptr) {
head->prev = [1];
}
delete temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting head->prev to head or temp instead of nullptr.
Forgetting to update head->prev after deletion.
✗ Incorrect
The previous pointer of the new head must be set to nullptr, not to head or any other pointer.
4fill in blank
hardFill both blanks to correctly delete the first node and update pointers.
DSA C++
void deleteBeginning(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->[1];
if (head != nullptr) {
head->[2] = nullptr;
}
delete temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping next and prev pointers.
Not setting head->prev to nullptr.
✗ Incorrect
Move head to head->next and set head->prev to nullptr to maintain list integrity.
5fill in blank
hardFill all three blanks to implement deletion of the first node in a doubly linked list.
DSA C++
void deleteBeginning(Node*& head) {
if (head == nullptr) return;
Node* [1] = head;
head = head->[2];
if (head != nullptr) {
head->[3] = nullptr;
}
delete temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names.
Not updating pointers correctly.
✗ Incorrect
Use a temporary pointer 'temp' to hold the old head, move head to head->next, and set head->prev to nullptr.
