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 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
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* deleteBeginning(Node* head) {
if (head == NULL) return NULL;
Node* temp = head;
head = head->next;
if (head != NULL) head->prev = NULL;
free(temp);
return head;
}
int main() {
Node* head = malloc(sizeof(Node));
Node* second = malloc(sizeof(Node));
Node* third = malloc(sizeof(Node));
head->data = 10; head->prev = NULL; head->next = second;
second->data = 20; second->prev = head; second->next = third;
third->data = 30; third->prev = second; third->next = NULL;
head = deleteBeginning(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Deleting the first node removes the node with data 10 and updates the head.
✗ Incorrect
The function deletes the first node (10), updates head to second node (20), and sets its prev to NULL. The list becomes 20 <-> 30 <-> NULL.
❓ Predict Output
intermediate2:00remaining
Output when deleting from an empty doubly linked list
What is the output when trying to delete the first node from an empty doubly linked list?
DSA C
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
void printList(Node* head) {
if (head == NULL) {
printf("List is empty\n");
return;
}
Node* temp = head;
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
Node* deleteBeginning(Node* head) {
if (head == NULL) return NULL;
Node* temp = head;
head = head->next;
if (head != NULL) head->prev = NULL;
free(temp);
return head;
}
int main() {
Node* head = NULL;
head = deleteBeginning(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Check if the list is empty before deleting.
✗ Incorrect
Since the list is empty (head is NULL), deleteBeginning returns NULL and printList prints 'List is empty'.
🔧 Debug
advanced2:00remaining
Identify the bug in deleting the first node of a doubly linked list
What is the bug in the following deleteBeginning function for a doubly linked list?
DSA C
Node* deleteBeginning(Node* head) {
if (head == NULL) return NULL;
Node* temp = head;
head = head->next;
head->prev = NULL;
free(temp);
return head;
}Attempts:
2 left
💡 Hint
Consider what happens when head->next is NULL.
✗ Incorrect
If the list has only one node, head->next is NULL, so head->prev = NULL causes a segmentation fault because head is NULL after assignment.
🧠 Conceptual
advanced2:00remaining
Effect of deleting the first node on the 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 changes when the first node is removed.
✗ Incorrect
The head pointer must point to the new first node, and the new head's prev pointer must be set to NULL to indicate it has no previous node.
❓ Predict Output
expert3:00remaining
Output after multiple deletions from the beginning of a doubly linked list
What is the printed state of the doubly linked list after deleting the first node 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* deleteBeginning(Node* head) {
if (head == NULL) return NULL;
Node* temp = head;
head = head->next;
if (head != NULL) head->prev = NULL;
free(temp);
return head;
}
int main() {
Node* head = malloc(sizeof(Node));
Node* second = malloc(sizeof(Node));
Node* third = malloc(sizeof(Node));
head->data = 5; head->prev = NULL; head->next = second;
second->data = 15; second->prev = head; second->next = third;
third->data = 25; third->prev = second; third->next = NULL;
head = deleteBeginning(head);
head = deleteBeginning(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Deleting twice removes first two nodes.
✗ Incorrect
First deletion removes node with data 5, second removes node with data 15, leaving node with data 25 as head.
