Challenge - 5 Problems
Doubly Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after deleting a value from doubly linked list
What is the printed state of the doubly linked list after deleting the first occurrence of value 3?
DSA C
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
Node* deleteByValue(Node* head, int val) {
Node* temp = head;
while (temp != NULL && temp->data != val) {
temp = temp->next;
}
if (temp == NULL) return head;
if (temp->prev != NULL) temp->prev->next = temp->next;
else head = temp->next;
if (temp->next != NULL) temp->next->prev = temp->prev;
free(temp);
return head;
}
int main() {
Node* head = malloc(sizeof(Node));
Node* second = malloc(sizeof(Node));
Node* third = malloc(sizeof(Node));
Node* fourth = malloc(sizeof(Node));
head->data = 1; head->prev = NULL; head->next = second;
second->data = 3; second->prev = head; second->next = third;
third->data = 5; third->prev = second; third->next = fourth;
fourth->data = 3; fourth->prev = third; fourth->next = NULL;
head = deleteByValue(head, 3);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Remember, only the first occurrence of the value is deleted.
✗ Incorrect
The function deletes the first node with value 3, which is the second node. The list after deletion is 1 <-> 5 <-> 3 <-> NULL.
🧠 Conceptual
intermediate1:00remaining
Number of nodes after deleting a value
If a doubly linked list has 5 nodes and you delete the first occurrence of a value present in the list, how many nodes remain?
Attempts:
2 left
💡 Hint
Deleting one node removes exactly one node from the list.
✗ Incorrect
Deleting one node reduces the total count by one, so 5 - 1 = 4 nodes remain.
🔧 Debug
advanced2:00remaining
Identify the bug in deleteByValue function
What is the bug in this deleteByValue function for doubly linked list that causes a crash when deleting the head node?
Node* deleteByValue(Node* head, int val) {
Node* temp = head;
while (temp != NULL && temp->data != val) {
temp = temp->next;
}
if (temp == NULL) return head;
temp->prev->next = temp->next;
if (temp->next != NULL) temp->next->prev = temp->prev;
free(temp);
return head;
}
Attempts:
2 left
💡 Hint
Think about what happens if the node to delete is the first node.
✗ Incorrect
If temp is the head node, temp->prev is NULL. Accessing temp->prev->next causes a crash.
❓ Predict Output
advanced2:00remaining
Output after deleting a non-existing value
What is the printed state of the doubly linked list after attempting to delete value 10 which is not present?
DSA C
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
Node* deleteByValue(Node* head, int val) {
Node* temp = head;
while (temp != NULL && temp->data != val) {
temp = temp->next;
}
if (temp == NULL) return head;
if (temp->prev != NULL) temp->prev->next = temp->next;
else head = temp->next;
if (temp->next != NULL) temp->next->prev = temp->prev;
free(temp);
return head;
}
int main() {
Node* head = malloc(sizeof(Node));
Node* second = malloc(sizeof(Node));
Node* third = malloc(sizeof(Node));
head->data = 2; head->prev = NULL; head->next = second;
second->data = 4; second->prev = head; second->next = third;
third->data = 6; third->prev = second; third->next = NULL;
head = deleteByValue(head, 10);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
If the value is not found, the list remains unchanged.
✗ Incorrect
Since 10 is not in the list, no deletion happens and the list stays the same.
🚀 Application
expert3:00remaining
Resulting list after multiple deletions by value
Given a doubly linked list with values 7 <-> 3 <-> 7 <-> 5 <-> 7 <-> NULL, what is the printed list after deleting the first occurrence of 7 twice in a row?
DSA C
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
Node* deleteByValue(Node* head, int val) {
Node* temp = head;
while (temp != NULL && temp->data != val) {
temp = temp->next;
}
if (temp == NULL) return head;
if (temp->prev != NULL) temp->prev->next = temp->next;
else head = temp->next;
if (temp->next != NULL) temp->next->prev = temp->prev;
free(temp);
return head;
}
int main() {
Node* head = malloc(sizeof(Node));
Node* n2 = malloc(sizeof(Node));
Node* n3 = malloc(sizeof(Node));
Node* n4 = malloc(sizeof(Node));
Node* n5 = malloc(sizeof(Node));
head->data = 7; head->prev = NULL; head->next = n2;
n2->data = 3; n2->prev = head; n2->next = n3;
n3->data = 7; n3->prev = n2; n3->next = n4;
n4->data = 5; n4->prev = n3; n4->next = n5;
n5->data = 7; n5->prev = n4; n5->next = NULL;
head = deleteByValue(head, 7);
head = deleteByValue(head, 7);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Each deletion removes the first 7 found from the current list.
✗ Incorrect
First deletion removes the head 7, list becomes 3 <-> 7 <-> 5 <-> 7 <-> NULL. Second deletion removes the next 7 (second node), list becomes 3 <-> 5 <-> 7 <-> NULL.
