Bird
Raised Fist0
DSA Cprogramming~20 mins

Delete by Value in Doubly Linked List in DSA C - Practice Problems & Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What happens when you delete a node by value in a doubly linked list?
easy
A. Only the first node is deleted without checking value
B. The node with the matching value is removed and links are updated
C. All nodes are deleted regardless of value
D. The list is reversed instead of deleting

Solution

  1. Step 1: Locate the node with the given value

    Traverse the list from head to tail to find the node whose data matches the value to delete.
  2. Step 2: Update links to remove the node

    Adjust the previous node's next pointer and the next node's previous pointer to bypass the node to be deleted.
  3. Final Answer:

    The node with the matching value is removed and links are updated -> Option B
  4. Quick Check:

    Deleting by value removes matching node [OK]
Hint: Find node by value, then fix links to remove it [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Not updating next node's previous pointer
  • Deleting wrong node without checking value
2. Which of the following is the correct syntax to delete a node by value in a doubly linked list in C?
easy
A. void deleteByValue(Node** head, int val) { /* code */ }
B. int deleteByValue(Node head, int val) { /* code */ }
C. void deleteByValue(Node* head, int val) { /* code */ }
D. Node deleteByValue(Node** head, int val) { /* code */ }

Solution

  1. Step 1: Understand pointer usage for head modification

    Since deleting a node may change the head pointer, we pass a pointer to the head pointer (Node** head).
  2. Step 2: Confirm function return type

    The function deletes a node and does not return a node or int, so return type is void.
  3. Final Answer:

    void deleteByValue(Node** head, int val) { /* code */ } -> Option A
  4. Quick Check:

    Use double pointer to modify head [OK]
Hint: Use Node** to allow head pointer changes [OK]
Common Mistakes:
  • Passing head as single pointer when head may change
  • Returning wrong type from delete function
  • Not using void when no return needed
3. What will be the output list after deleting value 3 from the doubly linked list: 1 <-> 3 <-> 5 <-> 3 <-> 7?
medium
A. 1 <-> 3 <-> 5 <-> 7
B. 3 <-> 5 <-> 3 <-> 7
C. 1 <-> 5 <-> 7
D. 1 <-> 5 <-> 3 <-> 7

Solution

  1. Step 1: Identify which node is deleted

    Deleting by value removes the first node with value 3, which is the second node in the list.
  2. Step 2: Update links and list after deletion

    After removing the first 3, the list becomes 1 <-> 5 <-> 3 <-> 7, keeping the second 3 intact.
  3. Final Answer:

    1 <-> 5 <-> 3 <-> 7 -> Option D
  4. Quick Check:

    Only first matching node deleted [OK]
Hint: Delete only first matching node by value [OK]
Common Mistakes:
  • Deleting all nodes with value 3 instead of first
  • Not updating links correctly after deletion
  • Deleting wrong node (like last 3 instead of first)
4. Given this code snippet to delete a node by value, what is the error?
void deleteByValue(Node** head, int val) {
  Node* temp = *head;
  while (temp != NULL && temp->data != val) {
    temp = temp->next;
  }
  if (temp == NULL) return;
  if (temp->prev != NULL) temp->prev->next = temp->next;
  if (temp->next != NULL) temp->next->prev = temp->prev;
  free(temp);
}
medium
A. Loops infinitely when value not found
B. Does not free memory of deleted node
C. Does not update head if first node is deleted
D. Deletes all nodes with matching value

Solution

  1. Step 1: Check head update when deleting first node

    If the node to delete is the head, the head pointer must be updated to the next node, but this code does not do that.
  2. Step 2: Confirm other parts are correct

    Memory is freed, loop ends correctly, and only one node is deleted, so no other errors.
  3. Final Answer:

    Does not update head if first node is deleted -> Option C
  4. Quick Check:

    Update head pointer when deleting first node [OK]
Hint: Always update head if deleting first node [OK]
Common Mistakes:
  • Forgetting to update head pointer
  • Not freeing memory after deletion
  • Incorrect loop causing infinite iteration
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30 <-> 40. You want to delete the node with value 10 and then insert a new node with value 5 at the head. What will be the final list?
hard
A. 5 <-> 20 <-> 30 <-> 40
B. 10 <-> 5 <-> 20 <-> 30 <-> 40
C. 20 <-> 30 <-> 40 <-> 5
D. 5 <-> 10 <-> 20 <-> 30 <-> 40

Solution

  1. Step 1: Delete node with value 10 (head node)

    Removing the head node 10 updates the head pointer to node 20, so list becomes 20 <-> 30 <-> 40.
  2. Step 2: Insert new node with value 5 at head

    Create a new node with value 5, set its next to current head (20), update 20's prev to new node, and update head pointer to new node.
  3. Step 3: Final list after operations

    The list is now 5 <-> 20 <-> 30 <-> 40.
  4. Final Answer:

    5 <-> 20 <-> 30 <-> 40 -> Option A
  5. Quick Check:

    Delete head, then insert new head [OK]
Hint: Delete head, then insert new node at head [OK]
Common Mistakes:
  • Not updating head after deletion
  • Not linking new node correctly at head
  • Appending new node at tail instead of head
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard