Bird
Raised Fist0
DSA Cprogramming~20 mins

Delete Node by Value in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
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 node by value in a singly linked list
What is the printed linked list after deleting the node with value 3?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

void printList(Node* head) {
    Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

Node* deleteNodeByValue(Node* head, int value) {
    Node* temp = head;
    Node* prev = NULL;
    while (temp != NULL && temp->data != value) {
        prev = temp;
        temp = temp->next;
    }
    if (temp == NULL) return head;
    if (prev == NULL) {
        head = temp->next;
    } else {
        prev->next = temp->next;
    }
    free(temp);
    return head;
}

int main() {
    Node* head = malloc(sizeof(Node));
    head->data = 1;
    head->next = malloc(sizeof(Node));
    head->next->data = 2;
    head->next->next = malloc(sizeof(Node));
    head->next->next->data = 3;
    head->next->next->next = malloc(sizeof(Node));
    head->next->next->next->data = 4;
    head->next->next->next->next = NULL;

    head = deleteNodeByValue(head, 3);
    printList(head);
    return 0;
}
A1 -> 2 -> 3 -> 4 -> NULL
B1 -> 2 -> 4 -> NULL
C2 -> 3 -> 4 -> NULL
D1 -> 3 -> 4 -> NULL
Attempts:
2 left
💡 Hint
Think about how the node with value 3 is removed and how the links are updated.
❓ Predict Output
intermediate
2:00remaining
Output when deleting the head node by value
What is the printed linked list after deleting the node with value 1 (the head)?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

void printList(Node* head) {
    Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

Node* deleteNodeByValue(Node* head, int value) {
    Node* temp = head;
    Node* prev = NULL;
    while (temp != NULL && temp->data != value) {
        prev = temp;
        temp = temp->next;
    }
    if (temp == NULL) return head;
    if (prev == NULL) {
        head = temp->next;
    } else {
        prev->next = temp->next;
    }
    free(temp);
    return head;
}

int main() {
    Node* head = malloc(sizeof(Node));
    head->data = 1;
    head->next = malloc(sizeof(Node));
    head->next->data = 2;
    head->next->next = malloc(sizeof(Node));
    head->next->next->data = 3;
    head->next->next->next = NULL;

    head = deleteNodeByValue(head, 1);
    printList(head);
    return 0;
}
A3 -> NULL
B1 -> 2 -> 3 -> NULL
CNULL
D2 -> 3 -> NULL
Attempts:
2 left
💡 Hint
Deleting the head means the new head is the next node.
🔧 Debug
advanced
2:00remaining
Identify the error in deleting a node by value
What error will this code cause when deleting a node by value?
DSA C
Node* deleteNodeByValue(Node* head, int value) {
    Node* temp = head;
    Node* prev = NULL;
    while (temp->data != value) {
        prev = temp;
        temp = temp->next;
    }
    if (prev == NULL) {
        head = temp->next;
    } else {
        prev->next = temp->next;
    }
    free(temp);
    return head;
}
ASegmentation fault (accessing NULL pointer)
BMemory leak (node not freed)
CNo error, works correctly
DCompilation error (missing return statement)
Attempts:
2 left
💡 Hint
Consider what happens if the value is not found in the list.
🧠 Conceptual
advanced
1:00remaining
Effect of deleting a non-existent value in linked list
What happens if you try to delete a node with a value not present in the linked list?
AThe list remains unchanged
BThe last node is deleted
CThe head node is deleted
DThe entire list is deleted
Attempts:
2 left
💡 Hint
Think about the search process for the value.
❓ Predict Output
expert
3:00remaining
Output after multiple deletions by value in linked list
What is the printed linked list after deleting nodes with values 2 and 4 in order?
DSA C
typedef struct Node {
    int data;
    struct Node* next;
} Node;

#include <stdio.h>
#include <stdlib.h>

void printList(Node* head) {
    Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

Node* deleteNodeByValue(Node* head, int value) {
    Node* temp = head;
    Node* prev = NULL;
    while (temp != NULL && temp->data != value) {
        prev = temp;
        temp = temp->next;
    }
    if (temp == NULL) return head;
    if (prev == NULL) {
        head = temp->next;
    } else {
        prev->next = temp->next;
    }
    free(temp);
    return head;
}

int main() {
    Node* head = malloc(sizeof(Node));
    head->data = 1;
    head->next = malloc(sizeof(Node));
    head->next->data = 2;
    head->next->next = malloc(sizeof(Node));
    head->next->next->data = 3;
    head->next->next->next = malloc(sizeof(Node));
    head->next->next->next->data = 4;
    head->next->next->next->next = malloc(sizeof(Node));
    head->next->next->next->next->data = 5;
    head->next->next->next->next->next = NULL;

    head = deleteNodeByValue(head, 2);
    head = deleteNodeByValue(head, 4);
    printList(head);
    return 0;
}
A1 -> 2 -> 3 -> 5 -> NULL
B3 -> 5 -> NULL
C1 -> 3 -> 5 -> NULL
D1 -> 3 -> 4 -> 5 -> NULL
Attempts:
2 left
💡 Hint
Delete nodes one by one and update the list each time.

Practice

(1/5)
1. What is the main purpose of the Delete Node by Value operation in a linked list?
easy
A. To remove the first node that contains the given value from the list
B. To add a new node with the given value at the end of the list
C. To find the position of a node with the given value
D. To reverse the linked list

Solution

  1. Step 1: Understand the operation's goal

    The operation aims to remove a node that matches a specific value from the linked list.
  2. Step 2: Identify the correct description

    Removing the first node with the given value matches To remove the first node that contains the given value from the list, while others describe different operations.
  3. Final Answer:

    To remove the first node that contains the given value from the list -> Option A
  4. Quick Check:

    Delete Node by Value = Remove matching node [OK]
Hint: Deleting by value means removing the first matching node [OK]
Common Mistakes:
  • Confusing deletion by value with insertion
  • Thinking it reverses the list
  • Assuming it finds position only
2. Which of the following is the correct way to start deleting a node by value in a singly linked list in C?
easy
A. Reverse the list before deletion
B. Directly free the head pointer without checks
C. Create a new node with the value to delete
D. Set a pointer to the head and check if head's value matches

Solution

  1. Step 1: Understand deletion start

    We must check if the head node itself contains the value to delete before moving forward.
  2. Step 2: Identify correct approach

    Setting a pointer to head and checking its value is the correct first step; other options are incorrect or irrelevant.
  3. Final Answer:

    Set a pointer to the head and check if head's value matches -> Option D
  4. Quick Check:

    Start at head to delete by value [OK]
Hint: Always check head node first when deleting by value [OK]
Common Mistakes:
  • Freeing head without checking value
  • Creating new nodes unnecessarily
  • Reversing list before deletion
3. Consider the following linked list: 10 -> 20 -> 30 -> 40 -> NULL. After deleting the node with value 30, what will be the new list?
medium
A. 10 -> 30 -> 40 -> NULL
B. 10 -> 20 -> 40 -> NULL
C. 20 -> 30 -> 40 -> NULL
D. 10 -> 20 -> 30 -> 40 -> NULL

Solution

  1. Step 1: Locate node with value 30

    Traverse the list: 10, 20, then 30 is found as the third node.
  2. Step 2: Remove node 30 and link previous to next

    Link node with value 20 directly to node with value 40, skipping 30.
  3. Final Answer:

    10 -> 20 -> 40 -> NULL -> Option B
  4. Quick Check:

    Deleted 30, list skips it [OK]
Hint: Link previous node to next to remove target node [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Deleting wrong node
  • Leaving deleted node in list
4. Given this code snippet to delete a node by value, what is the main error?
void deleteNode(Node** head, int val) {
  Node* temp = *head;
  Node* prev = NULL;
  while (temp != NULL && temp->data != val) {
    prev = temp;
    temp = temp->next;
  }
  free(temp);
  if (prev != NULL) {
    prev->next = temp->next;
  } else {
    *head = temp->next;
  }
}
medium
A. prev pointer is never updated
B. The loop condition is incorrect and never runs
C. free(temp) is called before updating pointers, causing invalid access
D. The function does not handle empty list

Solution

  1. Step 1: Analyze order of free and pointer update

    The code frees temp before updating prev->next or head, so temp->next access is invalid.
  2. Step 2: Identify correct fix

    Pointers must be updated before freeing the node to avoid accessing freed memory.
  3. Final Answer:

    free(temp) is called before updating pointers, causing invalid access -> Option C
  4. Quick Check:

    Free after pointer update to avoid errors [OK]
Hint: Update pointers before freeing node to avoid invalid access [OK]
Common Mistakes:
  • Freeing node before fixing links
  • Ignoring NULL list case
  • Not updating prev pointer
5. You have a singly linked list: 5 -> 10 -> 15 -> 20 -> 25 -> NULL. Write a function to delete all nodes with value greater than 15. What will be the final list after deletion?
hard
A. 5 -> 10 -> 15 -> NULL
B. 5 -> 10 -> 20 -> 25 -> NULL
C. 15 -> 20 -> 25 -> NULL
D. 5 -> 10 -> 15 -> 20 -> 25 -> NULL

Solution

  1. Step 1: Identify nodes with value > 15

    Nodes with values 20 and 25 are greater than 15 and must be deleted.
  2. Step 2: Delete nodes and link remaining nodes

    After removing 20 and 25, the list becomes 5 -> 10 -> 15 -> NULL.
  3. Final Answer:

    5 -> 10 -> 15 -> NULL -> Option A
  4. Quick Check:

    Removed nodes > 15, list ends at 15 [OK]
Hint: Delete nodes by checking value > 15 and relink list [OK]
Common Mistakes:
  • Deleting wrong nodes
  • Not updating links after deletion
  • Assuming only one node deleted
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