Challenge - 5 Problems
Doubly Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting last node from doubly linked list
What is the printed state of the doubly linked list after deleting the last node?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
};
void deleteFromEnd(Node*& head) {
if (!head) return;
if (!head->next) {
delete head;
head = nullptr;
return;
}
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->prev->next = nullptr;
delete temp;
}
void printList(Node* head) {
while (head) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* head = new Node{1, nullptr, nullptr};
head->next = new Node{2, head, nullptr};
head->next->next = new Node{3, head->next, nullptr};
deleteFromEnd(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Think about what happens to the last node and its previous node's next pointer.
✗ Incorrect
Deleting from the end removes the last node (3). The previous node (2) now points to null, so the list ends at 2.
❓ Predict Output
intermediateOutput after deleting last node from single-node doubly linked list
What is the printed state of the doubly linked list after deleting the last node when the list has only one node?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
};
void deleteFromEnd(Node*& head) {
if (!head) return;
if (!head->next) {
delete head;
head = nullptr;
return;
}
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->prev->next = nullptr;
delete temp;
}
void printList(Node* head) {
while (head) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* head = new Node{10, nullptr, nullptr};
deleteFromEnd(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
✗ Incorrect
When the list has only one node, deleting from the end deletes that node and sets head to nullptr, so the list is empty.
🔧 Debug
advancedIdentify the error in deleteFromEnd function
What error will occur when running this deleteFromEnd function on a doubly linked list with multiple nodes?
DSA C++
void deleteFromEnd(Node*& head) {
if (!head) return;
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = nullptr;
delete temp;
}Attempts:
2 left
💡 Hint
Check what happens to the previous node's next pointer after deletion.
✗ Incorrect
The function deletes the last node but does not update the previous node's next pointer to nullptr, leaving a dangling pointer and causing runtime errors.
❓ Predict Output
advancedOutput after deleting last node twice from doubly linked list
What is the printed state of the doubly linked list after deleting the last node twice in a row?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
};
void deleteFromEnd(Node*& head) {
if (!head) return;
if (!head->next) {
delete head;
head = nullptr;
return;
}
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->prev->next = nullptr;
delete temp;
}
void printList(Node* head) {
while (head) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
int main() {
Node* head = new Node{5, nullptr, nullptr};
head->next = new Node{10, head, nullptr};
head->next->next = new Node{15, head->next, nullptr};
deleteFromEnd(head);
deleteFromEnd(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Each deletion removes the last node, so after two deletions, one node remains.
✗ Incorrect
First deletion removes 15, second removes 10, leaving 5 as the only node.
🧠 Conceptual
expertWhy is updating the previous node's next pointer necessary when deleting from end?
In a doubly linked list, why must we update the previous node's next pointer to nullptr when deleting the last node?
Attempts:
2 left
💡 Hint
Think about what happens if the previous node still points to the deleted node.
✗ Incorrect
If the previous node's next pointer is not set to nullptr, it points to memory that was freed, causing crashes or unpredictable behavior.
