Bird
0
0
DSA Cprogramming~20 mins

Delete by Value in 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 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;
}
A1 <-> 5 <-> NULL
B1 <-> 3 <-> 5 <-> NULL
C1 <-> 5 <-> 3 <-> NULL
D3 <-> 5 <-> 3 <-> NULL
Attempts:
2 left
💡 Hint
Remember, only the first occurrence of the value is deleted.
🧠 Conceptual
intermediate
1: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?
A4
B5
C3
D0
Attempts:
2 left
💡 Hint
Deleting one node removes exactly one node from the list.
🔧 Debug
advanced
2: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; }
ANot updating head when deleting the last node
BAccessing temp->prev->next without checking if temp->prev is NULL
CNot freeing the memory of the deleted node
DLoop condition should be while(temp->next != NULL)
Attempts:
2 left
💡 Hint
Think about what happens if the node to delete is the first node.
Predict Output
advanced
2: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;
}
A2 <-> 4 <-> 6 <-> NULL
B2 <-> 6 <-> NULL
C4 <-> 6 <-> NULL
DNULL
Attempts:
2 left
💡 Hint
If the value is not found, the list remains unchanged.
🚀 Application
expert
3: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;
}
A7 <-> 3 <-> 5 <-> NULL
B5 <-> 7 <-> NULL
C3 <-> 7 <-> 5 <-> NULL
D3 <-> 5 <-> 7 <-> NULL
Attempts:
2 left
💡 Hint
Each deletion removes the first 7 found from the current list.