Bird
0
0
DSA Cprogramming~20 mins

Delete from End of Doubly Linked List in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A1 <-> 2 <-> 3 <-> NULL
B1 <-> 2 <-> NULL
C1 <-> NULL
DNULL
Attempts:
2 left
💡 Hint
Think about what happens to the last node and the second last node's next pointer.
🧠 Conceptual
intermediate
1: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
}
AReturns NULL without any error
BDeletes a random node
CCauses a segmentation fault
DReturns the original head pointer
Attempts:
2 left
💡 Hint
Check the first condition in the function.
🔧 Debug
advanced
2: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;
}
AMissing free(head) when list has one node
BNo bug, code is correct
CShould update head pointer after deletion
Dtemp->next = NULL; should be temp->prev->next = NULL;
Attempts:
2 left
💡 Hint
Check how the previous node's next pointer is updated before freeing the last node.
Predict Output
advanced
2: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;
}
A10
B10 <-> NULL
CNULL
DSegmentation fault
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
🧠 Conceptual
expert
1: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?
AO(n), because you must traverse to the last node
BO(1), because you can access the last node directly
CO(log n), because of binary search on nodes
DO(n^2), because of nested traversal
Attempts:
2 left
💡 Hint
Without a tail pointer, you must start from head and move forward to find the last node.