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 a value from a doubly linked list
What is the printed state of the doubly linked list after deleting the first occurrence of value 3?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
class DoublyLinkedList {
public:
Node* head;
DoublyLinkedList() : head(nullptr) {}
void append(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next) temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
void deleteByValue(int val) {
Node* temp = head;
while (temp) {
if (temp->data == val) {
if (temp->prev) temp->prev->next = temp->next;
else head = temp->next;
if (temp->next) temp->next->prev = temp->prev;
delete temp;
break;
}
temp = temp->next;
}
}
void printList() {
Node* temp = head;
while (temp) {
std::cout << temp->data;
if (temp->next) std::cout << " -> ";
temp = temp->next;
}
std::cout << " -> null" << std::endl;
}
};
int main() {
DoublyLinkedList dll;
dll.append(1);
dll.append(2);
dll.append(3);
dll.append(4);
dll.append(3);
dll.deleteByValue(3);
dll.printList();
return 0;
}Attempts:
2 left
💡 Hint
Remember, only the first occurrence of the value is deleted.
✗ Incorrect
The deleteByValue function removes the first node with data 3. The list initially is 1 -> 2 -> 3 -> 4 -> 3 -> null. After deletion, the first 3 is removed, resulting in 1 -> 2 -> 4 -> 3 -> null.
❓ Predict Output
intermediateOutput after deleting head value in doubly linked list
What is the printed state of the doubly linked list after deleting the value 1 (head node)?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
class DoublyLinkedList {
public:
Node* head;
DoublyLinkedList() : head(nullptr) {}
void append(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next) temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
void deleteByValue(int val) {
Node* temp = head;
while (temp) {
if (temp->data == val) {
if (temp->prev) temp->prev->next = temp->next;
else head = temp->next;
if (temp->next) temp->next->prev = temp->prev;
delete temp;
break;
}
temp = temp->next;
}
}
void printList() {
Node* temp = head;
while (temp) {
std::cout << temp->data;
if (temp->next) std::cout << " -> ";
temp = temp->next;
}
std::cout << " -> null" << std::endl;
}
};
int main() {
DoublyLinkedList dll;
dll.append(1);
dll.append(2);
dll.append(3);
dll.deleteByValue(1);
dll.printList();
return 0;
}Attempts:
2 left
💡 Hint
Deleting the head node updates the head pointer.
✗ Incorrect
The head node with value 1 is deleted. The new head becomes the next node with value 2. The list is now 2 -> 3 -> null.
🔧 Debug
advancedIdentify the error in deleteByValue method
What error will occur when running this deleteByValue method if the value to delete is not found in the list?
DSA C++
void deleteByValue(int val) { Node* temp = head; while (temp->data != val) { temp = temp->next; } if (temp->prev) temp->prev->next = temp->next; else head = temp->next; if (temp->next) temp->next->prev = temp->prev; delete temp; }
Attempts:
2 left
💡 Hint
Consider what happens if the value is not in the list.
✗ Incorrect
If the value is not found, temp becomes nullptr. Accessing temp->data causes a segmentation fault (runtime error).
❓ Predict Output
advancedOutput after deleting last node by value
What is the printed state of the doubly linked list after deleting the last occurrence of value 5?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
class DoublyLinkedList {
public:
Node* head;
DoublyLinkedList() : head(nullptr) {}
void append(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next) temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
void deleteLastOccurrence(int val) {
Node* temp = head;
Node* last = nullptr;
while (temp) {
if (temp->data == val) last = temp;
temp = temp->next;
}
if (!last) return;
if (last->prev) last->prev->next = last->next;
else head = last->next;
if (last->next) last->next->prev = last->prev;
delete last;
}
void printList() {
Node* temp = head;
while (temp) {
std::cout << temp->data;
if (temp->next) std::cout << " -> ";
temp = temp->next;
}
std::cout << " -> null" << std::endl;
}
};
int main() {
DoublyLinkedList dll;
dll.append(5);
dll.append(1);
dll.append(5);
dll.append(3);
dll.append(5);
dll.deleteLastOccurrence(5);
dll.printList();
return 0;
}Attempts:
2 left
💡 Hint
Only the last node with value 5 is deleted.
✗ Incorrect
The last occurrence of 5 is the last node. After deletion, the list is 5 -> 1 -> 5 -> 3 -> null.
❓ Predict Output
expertOutput after multiple deletions by value in doubly linked list
What is the printed state of the doubly linked list after deleting values 2, 4, and 6 in sequence?
DSA C++
struct Node {
int data;
Node* prev;
Node* next;
Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};
class DoublyLinkedList {
public:
Node* head;
DoublyLinkedList() : head(nullptr) {}
void append(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next) temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
void deleteByValue(int val) {
Node* temp = head;
while (temp) {
if (temp->data == val) {
if (temp->prev) temp->prev->next = temp->next;
else head = temp->next;
if (temp->next) temp->next->prev = temp->prev;
delete temp;
break;
}
temp = temp->next;
}
}
void printList() {
Node* temp = head;
while (temp) {
std::cout << temp->data;
if (temp->next) std::cout << " -> ";
temp = temp->next;
}
std::cout << " -> null" << std::endl;
}
};
int main() {
DoublyLinkedList dll;
dll.append(1);
dll.append(2);
dll.append(3);
dll.append(4);
dll.append(5);
dll.append(6);
dll.deleteByValue(2);
dll.deleteByValue(4);
dll.deleteByValue(6);
dll.printList();
return 0;
}Attempts:
2 left
💡 Hint
Each deleteByValue removes the first occurrence of the value.
✗ Incorrect
After deleting 2, 4, and 6, the list contains 1, 3, and 5 only.
