Challenge - 5 Problems
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting a node by value in a singly linked list
What is the printed state of the linked list after deleting the node with value 3?
DSA C++
struct Node {
int data;
Node* next;
};
void deleteNode(Node*& head, int value) {
if (!head) return;
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* current = head;
while (current->next && current->next->data != value) {
current = current->next;
}
if (current->next) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
}
}
int main() {
Node* head = new Node{1, new Node{2, new Node{3, new Node{4, nullptr}}}};
deleteNode(head, 3);
Node* temp = head;
while (temp) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null";
return 0;
}Attempts:
2 left
💡 Hint
Remember to update the next pointer of the node before the deleted node.
✗ Incorrect
The node with value 3 is removed. The list links 2 directly to 4, so the output is 1 -> 2 -> 4 -> null.
❓ Predict Output
intermediateOutput after deleting the head node by value
What is the printed state of the linked list after deleting the node with value 1 (head node)?
DSA C++
struct Node {
int data;
Node* next;
};
void deleteNode(Node*& head, int value) {
if (!head) return;
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* current = head;
while (current->next && current->next->data != value) {
current = current->next;
}
if (current->next) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
}
}
int main() {
Node* head = new Node{1, new Node{2, new Node{3, nullptr}}};
deleteNode(head, 1);
Node* temp = head;
while (temp) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null";
return 0;
}Attempts:
2 left
💡 Hint
Deleting the head node means the head pointer must move to the next node.
✗ Incorrect
The head node with value 1 is deleted, so the new head is the node with value 2.
🔧 Debug
advancedIdentify the error in the deleteNode function
What error will occur when running this code if the value to delete is not in the list?
DSA C++
struct Node {
int data;
Node* next;
};
void deleteNode(Node*& head, int value) {
Node* current = head;
while (current->next->data != value) {
current = current->next;
}
Node* temp = current->next;
current->next = current->next->next;
delete temp;
}
int main() {
Node* head = new Node{1, new Node{2, new Node{3, nullptr}}};
deleteNode(head, 4);
return 0;
}Attempts:
2 left
💡 Hint
Check what happens when current->next is nullptr and you try to access current->next->data.
✗ Incorrect
If the value is not found, current->next becomes nullptr, so accessing current->next->data causes a segmentation fault.
🧠 Conceptual
advancedWhy is a double pointer or reference to pointer needed to delete the head node?
Why must the deleteNode function take the head pointer as a reference or double pointer when deleting a node by value?
Attempts:
2 left
💡 Hint
Think about what happens if you delete the first node without changing the head pointer.
✗ Incorrect
If the head node is deleted, the head pointer must be updated to the next node. Passing by reference or double pointer allows this change to affect the original head pointer.
🚀 Application
expertResulting list after multiple deletions by value
Given the initial list 5 -> 3 -> 7 -> 3 -> 9 -> null, what is the printed list after deleting all nodes with value 3 using the deleteNode function repeatedly?
DSA C++
struct Node {
int data;
Node* next;
};
void deleteNode(Node*& head, int value) {
if (!head) return;
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* current = head;
while (current->next && current->next->data != value) {
current = current->next;
}
if (current->next) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
}
}
int main() {
Node* head = new Node{5, new Node{3, new Node{7, new Node{3, new Node{9, nullptr}}}}};
deleteNode(head, 3);
deleteNode(head, 3);
Node* temp = head;
while (temp) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null";
return 0;
}Attempts:
2 left
💡 Hint
Each call deletes only one node with the given value. Two calls remove both nodes with value 3.
✗ Incorrect
Both nodes with value 3 are deleted in two calls, leaving nodes 5, 7, and 9 linked.
