Bird
Raised Fist0
DSA C++programming~10 mins

Delete Node by Value in DSA C++ - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the list is empty before deletion.

DSA C++
if (head == [1]) {
    return head;
}
Drag options to blanks, or click blank then click option'
A0
BNULL
Cnullptr
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or NULL instead of nullptr.
Checking head against head itself.
2fill in blank
medium

Complete the code to delete the head node if it matches the value.

DSA C++
if (head->data == [1]) {
    Node* temp = head;
    head = head->next;
    delete temp;
    return head;
}
Drag options to blanks, or click blank then click option'
Anode
Bval
Cdata
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'data' instead of 'value'.
Comparing with node pointer instead of data.
3fill in blank
hard

Fix the error in the while loop condition to traverse the list correctly.

DSA C++
while (current != [1] && current->data != value) {
    prev = current;
    current = current->next;
}
Drag options to blanks, or click blank then click option'
ANULL
Bnullptr
Chead
Dcurrent
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of nullptr.
Checking current against head or current itself.
4fill in blank
hard

Fill both blanks to correctly unlink and delete the target node.

DSA C++
if (current != nullptr) {
    [1]->next = current->[2];
    delete current;
}
Drag options to blanks, or click blank then click option'
Aprev
Bnext
Attempts:
3 left
💡 Hint
Common Mistakes
Using current->next instead of prev->next.
Mixing up prev and next pointers.
5fill in blank
hard

Fill all three blanks to complete the function that deletes a node by value.

DSA C++
Node* deleteNode(Node* head, int value) {
    if (head == [1]) return head;
    if (head->data == [2]) {
        Node* temp = head;
        head = head->next;
        delete temp;
        return head;
    }
    Node* prev = head;
    Node* current = head->next;
    while (current != [3] && current->data != value) {
        prev = current;
        current = current->next;
    }
    if (current != nullptr) {
        prev->next = current->next;
        delete current;
    }
    return head;
}
Drag options to blanks, or click blank then click option'
Anullptr
Bvalue
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of nullptr.
Comparing with wrong variable names.
Incorrect loop condition.

Practice

(1/5)
1. What is the main purpose of the Delete Node by Value operation in a singly linked list?
easy
A. Reverse the entire linked list
B. Add a new node with the given value at the end of the list
C. Remove the first node that contains the given value from the list
D. Find the position of a node with the given value

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 Remove the first node that contains the given value from the list, while others describe different operations.
  3. Final Answer:

    Remove the first node that contains the given value from the list -> Option C
  4. Quick Check:

    Delete node by value means removing matching node [OK]
Hint: Deleting means removing matching node, not adding or searching [OK]
Common Mistakes:
  • Confusing delete with add or search operations
  • Thinking it reverses the list
  • Assuming it deletes all nodes with the value
2. Which of the following is the correct C++ syntax to delete a node by value from a singly linked list?
easy
A. void deleteNode(int val, Node* head) { /* code */ }
B. Node deleteNode(Node* head, int val) { /* code */ }
C. int deleteNode(Node* head, int val) { /* code */ }
D. void deleteNode(Node* head, int val) { /* code */ }

Solution

  1. Step 1: Check parameter order and return type

    The function should take the head pointer first and the value to delete second, returning void since it modifies the list.
  2. Step 2: Match with options

    void deleteNode(Node* head, int val) { /* code */ } matches the correct parameter order and return type. Others have wrong order or return types.
  3. Final Answer:

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

    Head pointer first, value second, void return [OK]
Hint: Function deletes node, so return type is void and head pointer first [OK]
Common Mistakes:
  • Swapping parameter order
  • Using wrong return type
  • Returning Node instead of void
3. What will be the linked list after deleting the node with value 3 from the list: 1 -> 3 -> 5 -> 3 -> 7 -> null?
void deleteNode(Node*& head, int val) {
    if (!head) return;
    if (head->data == val) {
        Node* temp = head;
        head = head->next;
        delete temp;
        return;
    }
    Node* curr = head;
    while (curr->next && curr->next->data != val) {
        curr = curr->next;
    }
    if (curr->next) {
        Node* temp = curr->next;
        curr->next = curr->next->next;
        delete temp;
    }
}
medium
A. 1 -> 5 -> 3 -> 7 -> null
B. 1 -> 3 -> 5 -> 7 -> null
C. 1 -> 5 -> 7 -> null
D. 3 -> 5 -> 3 -> 7 -> null

Solution

  1. Step 1: Check if head node has value 3

    Head node is 1, not 3, so skip first if block.
  2. Step 2: Traverse to find first node with value 3

    Loop stops at node before first 3 (which is node with value 1). Then deletes first 3 node.
  3. Step 3: Update links after deletion

    After deletion, list becomes 1 -> 5 -> 3 -> 7 -> null.
  4. Final Answer:

    1 -> 5 -> 3 -> 7 -> null -> Option A
  5. Quick Check:

    First 3 deleted, rest unchanged [OK]
Hint: Delete only first matching node, update links carefully [OK]
Common Mistakes:
  • Deleting all nodes with value 3
  • Deleting wrong node (head vs next)
  • Not updating links correctly
4. Identify the bug in the following code snippet for deleting a node by value from a singly linked list:
void deleteNode(Node* head, int val) {
    if (!head) return;
    if (head->data == val) {
        Node* temp = head;
        head = head->next;
        delete temp;
        return;
    }
    Node* curr = head;
    while (curr->next && curr->next->data != val) {
        curr = curr->next;
    }
    if (curr->next) {
        Node* temp = curr->next;
        curr->next = curr->next->next;
        delete temp;
    }
}
medium
A. The code deletes all nodes with the value instead of first only
B. The head pointer is passed by value, so changes to head are lost
C. The loop condition is incorrect and causes infinite loop
D. The code does not delete the node properly, causing memory leak

Solution

  1. Step 1: Analyze head pointer parameter

    Head is passed by value, so changing head inside function does not affect original list.
  2. Step 2: Understand impact on deleting head node

    Deleting head node updates local head only, original list head remains unchanged, causing bug.
  3. Final Answer:

    The head pointer is passed by value, so changes to head are lost -> Option B
  4. Quick Check:

    Pass head by reference to update original list [OK]
Hint: Pass head by reference to delete head node correctly [OK]
Common Mistakes:
  • Not passing head by reference
  • Assuming head changes reflect outside function
  • Ignoring memory leaks
5. Given a singly linked list with possible duplicate values, how would you modify the deleteNode function to delete all nodes with a given value instead of just the first one? Choose the best approach.
hard
A. Use a loop to traverse the list and delete every node matching the value, updating links each time
B. Call the existing deleteNode function repeatedly until no node with the value remains
C. Only delete the head node if it matches, ignore others
D. Reverse the list first, then delete the first matching node

Solution

  1. Step 1: Understand requirement to delete all matching nodes

    We must traverse entire list and remove every node with the given value.
  2. Step 2: Choose approach that deletes all nodes in one pass

    Use a loop to traverse the list and delete every node matching the value, updating links each time uses a loop to check and delete nodes as we go, updating links properly.
  3. Step 3: Evaluate other options

    Call the existing deleteNode function repeatedly until no node with the value remains is inefficient and error-prone; C and D do not delete all nodes.
  4. Final Answer:

    Use a loop to traverse the list and delete every node matching the value, updating links each time -> Option A
  5. Quick Check:

    Loop and delete all matching nodes in one traversal [OK]
Hint: Traverse once, delete all matches by updating links carefully [OK]
Common Mistakes:
  • Deleting only first occurrence
  • Calling delete repeatedly without updating head
  • Ignoring link updates after deletion
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