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 the first node from a doubly linked list
What is the printed state of the doubly linked list after deleting the first node?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
void deleteBeginning(Node*& head) {
if (!head) return;
Node* temp = head;
head = head->next;
if (head) head->prev = 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);
head->next = new Node(20);
head->next->prev = head;
head->next->next = new Node(30);
head->next->next->prev = head->next;
deleteBeginning(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Deleting the first node moves the head pointer to the next node and updates its prev pointer.
✗ Incorrect
The first node with value 10 is deleted. The head moves to node with value 20, whose prev pointer is set to nullptr. The list now starts at 20 and continues to 30.
❓ Predict Output
intermediateOutput after deleting from an empty doubly linked list
What will be the output when deleting the first node from an empty doubly linked list and then printing it?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
void deleteBeginning(Node*& head) {
if (!head) return;
Node* temp = head;
head = head->next;
if (head) head->prev = nullptr;
delete temp;
}
void printList(Node* head) {
if (!head) std::cout << "null" << std::endl;
else {
while (head) {
std::cout << head->data << " <-> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
}
int main() {
Node* head = nullptr;
deleteBeginning(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Deleting from an empty list should do nothing and printing should show null.
✗ Incorrect
Since the list is empty (head is nullptr), deleteBeginning returns immediately. Printing shows null indicating an empty list.
🔧 Debug
advancedIdentify the error in deleting the first node of a doubly linked list
What error will this code cause when deleting the first node from a doubly linked list?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
void deleteBeginning(Node*& head) {
if (!head) return;
Node* temp = head;
head = head->next;
head->prev = nullptr; // Line with potential error
delete temp;
}
int main() {
Node* head = new Node(10);
deleteBeginning(head);
return 0;
}Attempts:
2 left
💡 Hint
Check what happens if head->next is nullptr before accessing head->prev.
✗ Incorrect
If the list has only one node, after head = head->next, head becomes nullptr. Accessing head->prev causes a runtime error (dereferencing nullptr).
🧠 Conceptual
advancedEffect of deleting the first node on doubly linked list pointers
After deleting the first node from a doubly linked list, which pointers must be updated to maintain list integrity?
Attempts:
2 left
💡 Hint
Think about what the head points to and what the new first node's prev pointer should be.
✗ Incorrect
The head pointer must move to the next node, and the new head's prev pointer must be set to nullptr to indicate it is the first node.
🚀 Application
expertResulting list after multiple deletions from beginning
Given a doubly linked list with nodes 5 <-> 15 <-> 25 <-> 35 <-> null, what is the printed list after deleting the first node twice?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
void deleteBeginning(Node*& head) {
if (!head) return;
Node* temp = head;
head = head->next;
if (head) head->prev = 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);
head->next = new Node(15);
head->next->prev = head;
head->next->next = new Node(25);
head->next->next->prev = head->next;
head->next->next->next = new Node(35);
head->next->next->next->prev = head->next->next;
deleteBeginning(head);
deleteBeginning(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Each deletion removes the first node and updates the head and prev pointers.
✗ Incorrect
First deletion removes 5, list becomes 15 <-> 25 <-> 35. Second deletion removes 15, list becomes 25 <-> 35.
