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 last node from doubly linked list
What is the printed state of the doubly linked list after deleting the last 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* deleteFromEnd(Node* head) {
if (head == NULL) return NULL;
if (head->next == NULL) {
free(head);
return NULL;
}
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->prev->next = NULL;
free(temp);
return head;
}
int main() {
Node* head = malloc(sizeof(Node));
Node* second = malloc(sizeof(Node));
Node* third = malloc(sizeof(Node));
head->data = 1; head->prev = NULL; head->next = second;
second->data = 2; second->prev = head; second->next = third;
third->data = 3; third->prev = second; third->next = NULL;
head = deleteFromEnd(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Think about what happens to the last node and the second last node's next pointer.
✗ Incorrect
The function deletes the last node (3). The second last node (2) now points to NULL, so the list is 1 <-> 2 <-> NULL.
🧠 Conceptual
intermediate1:30remaining
Effect of deleting from an empty doubly linked list
What is the result of calling the deleteFromEnd function on an empty doubly linked list (head is NULL)?
DSA C
Node* deleteFromEnd(Node* head) {
if (head == NULL) return NULL;
// rest of code omitted
}Attempts:
2 left
💡 Hint
Check the first condition in the function.
✗ Incorrect
If the list is empty (head is NULL), the function returns NULL immediately without errors.
🔧 Debug
advanced2:30remaining
Identify the bug in deleteFromEnd function
What is the bug in this deleteFromEnd function for doubly linked list?
DSA C
Node* deleteFromEnd(Node* head) {
if (head == NULL) return NULL;
if (head->next == NULL) {
free(head);
return NULL;
}
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = NULL;
free(temp);
return head;
}Attempts:
2 left
💡 Hint
Check how the previous node's next pointer is updated before freeing the last node.
✗ Incorrect
The code sets temp->next = NULL, but temp->next is already NULL (last node). It should set temp->prev->next = NULL to unlink the last node.
❓ Predict Output
advanced2:00remaining
Output after deleting last node from single-node doubly linked list
What is the printed state of the doubly linked list after deleting the last node when the list has only one 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* deleteFromEnd(Node* head) {
if (head == NULL) return NULL;
if (head->next == NULL) {
free(head);
return NULL;
}
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->prev->next = NULL;
free(temp);
return head;
}
int main() {
Node* head = malloc(sizeof(Node));
head->data = 10; head->prev = NULL; head->next = NULL;
head = deleteFromEnd(head);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
✗ Incorrect
The function frees the only node and returns NULL, so printing the list shows NULL.
🧠 Conceptual
expert1:30remaining
Time complexity of deleting from end in doubly linked list without tail pointer
What is the time complexity of deleting the last node from a doubly linked list if you do NOT maintain a tail pointer?
Attempts:
2 left
💡 Hint
Without a tail pointer, you must start from head and move forward to find the last node.
✗ Incorrect
Without a tail pointer, you traverse the list from head to last node, which takes linear time O(n).
