Bird
Raised Fist0
DSA C++programming~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 a doubly linked list
What is the printed state of the doubly linked list after deleting the first occurrence of value 3?
DSA C++
struct Node {
    int data;
    Node* prev;
    Node* next;
    Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};

class DoublyLinkedList {
public:
    Node* head;
    DoublyLinkedList() : head(nullptr) {}

    void append(int val) {
        Node* newNode = new Node(val);
        if (!head) {
            head = newNode;
            return;
        }
        Node* temp = head;
        while (temp->next) temp = temp->next;
        temp->next = newNode;
        newNode->prev = temp;
    }

    void deleteByValue(int val) {
        Node* temp = head;
        while (temp) {
            if (temp->data == val) {
                if (temp->prev) temp->prev->next = temp->next;
                else head = temp->next;
                if (temp->next) temp->next->prev = temp->prev;
                delete temp;
                break;
            }
            temp = temp->next;
        }
    }

    void printList() {
        Node* temp = head;
        while (temp) {
            std::cout << temp->data;
            if (temp->next) std::cout << " -> ";
            temp = temp->next;
        }
        std::cout << " -> null" << std::endl;
    }
};

int main() {
    DoublyLinkedList dll;
    dll.append(1);
    dll.append(2);
    dll.append(3);
    dll.append(4);
    dll.append(3);
    dll.deleteByValue(3);
    dll.printList();
    return 0;
}
A2 -> 3 -> 4 -> 3 -> null
B1 -> 2 -> 3 -> 4 -> null
C1 -> 2 -> 4 -> 3 -> null
D1 -> 2 -> 3 -> 4 -> 3 -> null
Attempts:
2 left
💡 Hint
Remember, only the first occurrence of the value is deleted.
❓ Predict Output
intermediate
2:00remaining
Output after deleting head value in doubly linked list
What is the printed state of the doubly linked list after deleting the value 1 (head node)?
DSA C++
struct Node {
    int data;
    Node* prev;
    Node* next;
    Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};

class DoublyLinkedList {
public:
    Node* head;
    DoublyLinkedList() : head(nullptr) {}

    void append(int val) {
        Node* newNode = new Node(val);
        if (!head) {
            head = newNode;
            return;
        }
        Node* temp = head;
        while (temp->next) temp = temp->next;
        temp->next = newNode;
        newNode->prev = temp;
    }

    void deleteByValue(int val) {
        Node* temp = head;
        while (temp) {
            if (temp->data == val) {
                if (temp->prev) temp->prev->next = temp->next;
                else head = temp->next;
                if (temp->next) temp->next->prev = temp->prev;
                delete temp;
                break;
            }
            temp = temp->next;
        }
    }

    void printList() {
        Node* temp = head;
        while (temp) {
            std::cout << temp->data;
            if (temp->next) std::cout << " -> ";
            temp = temp->next;
        }
        std::cout << " -> null" << std::endl;
    }
};

int main() {
    DoublyLinkedList dll;
    dll.append(1);
    dll.append(2);
    dll.append(3);
    dll.deleteByValue(1);
    dll.printList();
    return 0;
}
A3 -> 2 -> null
Bnull
C1 -> 2 -> 3 -> null
D2 -> 3 -> null
Attempts:
2 left
💡 Hint
Deleting the head node updates the head pointer.
🔧 Debug
advanced
2:00remaining
Identify the error in deleteByValue method
What error will occur when running this deleteByValue method if the value to delete is not found in the list?
DSA C++
void deleteByValue(int val) {
    Node* temp = head;
    while (temp->data != val) {
        temp = temp->next;
    }
    if (temp->prev) temp->prev->next = temp->next;
    else head = temp->next;
    if (temp->next) temp->next->prev = temp->prev;
    delete temp;
}
ANo error, deletes the node correctly
BRuntime error: segmentation fault due to dereferencing nullptr
CCompilation error: missing return statement
DLogical error: deletes wrong node
Attempts:
2 left
💡 Hint
Consider what happens if the value is not in the list.
❓ Predict Output
advanced
2:00remaining
Output after deleting last node by value
What is the printed state of the doubly linked list after deleting the last occurrence of value 5?
DSA C++
struct Node {
    int data;
    Node* prev;
    Node* next;
    Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};

class DoublyLinkedList {
public:
    Node* head;
    DoublyLinkedList() : head(nullptr) {}

    void append(int val) {
        Node* newNode = new Node(val);
        if (!head) {
            head = newNode;
            return;
        }
        Node* temp = head;
        while (temp->next) temp = temp->next;
        temp->next = newNode;
        newNode->prev = temp;
    }

    void deleteLastOccurrence(int val) {
        Node* temp = head;
        Node* last = nullptr;
        while (temp) {
            if (temp->data == val) last = temp;
            temp = temp->next;
        }
        if (!last) return;
        if (last->prev) last->prev->next = last->next;
        else head = last->next;
        if (last->next) last->next->prev = last->prev;
        delete last;
    }

    void printList() {
        Node* temp = head;
        while (temp) {
            std::cout << temp->data;
            if (temp->next) std::cout << " -> ";
            temp = temp->next;
        }
        std::cout << " -> null" << std::endl;
    }
};

int main() {
    DoublyLinkedList dll;
    dll.append(5);
    dll.append(1);
    dll.append(5);
    dll.append(3);
    dll.append(5);
    dll.deleteLastOccurrence(5);
    dll.printList();
    return 0;
}
A5 -> 1 -> 5 -> 3 -> null
B1 -> 5 -> 3 -> 5 -> null
C5 -> 1 -> 3 -> 5 -> null
D5 -> 1 -> 5 -> 3 -> 5 -> null
Attempts:
2 left
💡 Hint
Only the last node with value 5 is deleted.
❓ Predict Output
expert
3:00remaining
Output after multiple deletions by value in doubly linked list
What is the printed state of the doubly linked list after deleting values 2, 4, and 6 in sequence?
DSA C++
struct Node {
    int data;
    Node* prev;
    Node* next;
    Node(int val) : data(val), prev(nullptr), next(nullptr) {}
};

class DoublyLinkedList {
public:
    Node* head;
    DoublyLinkedList() : head(nullptr) {}

    void append(int val) {
        Node* newNode = new Node(val);
        if (!head) {
            head = newNode;
            return;
        }
        Node* temp = head;
        while (temp->next) temp = temp->next;
        temp->next = newNode;
        newNode->prev = temp;
    }

    void deleteByValue(int val) {
        Node* temp = head;
        while (temp) {
            if (temp->data == val) {
                if (temp->prev) temp->prev->next = temp->next;
                else head = temp->next;
                if (temp->next) temp->next->prev = temp->prev;
                delete temp;
                break;
            }
            temp = temp->next;
        }
    }

    void printList() {
        Node* temp = head;
        while (temp) {
            std::cout << temp->data;
            if (temp->next) std::cout << " -> ";
            temp = temp->next;
        }
        std::cout << " -> null" << std::endl;
    }
};

int main() {
    DoublyLinkedList dll;
    dll.append(1);
    dll.append(2);
    dll.append(3);
    dll.append(4);
    dll.append(5);
    dll.append(6);
    dll.deleteByValue(2);
    dll.deleteByValue(4);
    dll.deleteByValue(6);
    dll.printList();
    return 0;
}
A1 -> 3 -> 5 -> null
B3 -> 5 -> null
C1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
D1 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Each deleteByValue removes the first occurrence of the value.

Practice

(1/5)
1. What happens when you delete a node by value in a doubly linked list?
easy
A. All nodes are deleted regardless of value.
B. The node with the matching value is removed and links are updated.
C. Only the first node is deleted without checking 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:

    Delete by value removes matching node [OK]
Hint: Find node, update previous and next pointers [OK]
Common Mistakes:
  • Not updating both previous and next pointers
  • Deleting node without checking value
  • Deleting all nodes instead of one
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 deleteNode(Node* &head, int val) { /* code */ }
B. void deleteNode(int val) { /* code */ }
C. Node deleteNode(int val) { /* code */ }
D. int deleteNode(Node* head, int val) { /* code */ }

Solution

  1. Step 1: Understand parameter passing for head pointer

    Head pointer must be passed by reference to allow modification if the first node is deleted.
  2. Step 2: Check function return type

    Deletion functions usually return void as they modify the list in place.
  3. Final Answer:

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

    Pass head by reference and return void [OK]
Hint: Pass head by reference to modify list [OK]
Common Mistakes:
  • Passing head by value causing no list update
  • Returning Node instead of void
  • Missing head pointer parameter
3. What is the output of the following code snippet after deleting value 20 from the doubly linked list?
struct Node {
  int data;
  Node* prev;
  Node* next;
};

// List: 10 <-> 20 <-> 30
// deleteNode(head, 20);
// printList(head);
medium
A. 10 <-> null
B. 10 <-> 20 <-> 30 <-> null
C. 20 <-> 30 <-> null
D. 10 <-> 30 <-> null

Solution

  1. Step 1: Identify node to delete

    Node with value 20 is in the middle of the list 10 <-> 20 <-> 30.
  2. Step 2: Update links after deletion

    After deleting 20, 10's next points to 30, and 30's prev points to 10.
  3. Final Answer:

    10 <-> 30 <-> null -> Option D
  4. Quick Check:

    Middle node deleted, links updated [OK]
Hint: Deleting middle node links neighbors directly [OK]
Common Mistakes:
  • Not updating previous pointer of next node
  • Not updating next pointer of previous node
  • Leaving deleted node linked
4. Identify the bug in this delete by value function for doubly linked list:
void deleteNode(Node* &head, int val) {
  Node* temp = head;
  while (temp != nullptr && temp->data != val) {
    temp = temp->next;
  }
  if (temp == nullptr) return;
  if (temp->prev != nullptr) temp->prev->next = temp->next;
  if (temp->next != nullptr) temp->next->prev = temp->prev;
  delete temp;
}
medium
A. Does not check for null head pointer
B. Deletes all nodes with matching value
C. Does not update head if first node is deleted
D. Deletes node without freeing memory

Solution

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

    If the node to delete is the head, head pointer must be updated to next node.
  2. Step 2: Verify code for head update

    Code does not update head pointer when temp->prev is nullptr (first node).
  3. Final Answer:

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

    Head update missing for first node deletion [OK]
Hint: Update head if deleting first node [OK]
Common Mistakes:
  • Forgetting to update head pointer
  • Not handling empty list
  • Deleting without checking node existence
5. Given a doubly linked list with nodes: 5 <-> 10 <-> 15 <-> 20, what will be the list after deleting value 5 and then value 20?
hard
A. 10 <-> 15 <-> null
B. 5 <-> 10 <-> 15 <-> 20 <-> null
C. 10 <-> 15 <-> 20 <-> null
D. 15 <-> null

Solution

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

    Update head to 10, set 10's prev to nullptr. List becomes 10 <-> 15 <-> 20.
  2. Step 2: Delete node with value 20 (tail node)

    Update 15's next to nullptr. List becomes 10 <-> 15.
  3. Final Answer:

    10 <-> 15 <-> null -> Option A
  4. Quick Check:

    Deleted head and tail nodes correctly [OK]
Hint: Update head and tail pointers when deleting ends [OK]
Common Mistakes:
  • Not updating head when deleting first node
  • Not updating tail when deleting last node
  • Leaving dangling pointers
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