What if you could remove any item from your list instantly without breaking everything else?
Why Delete by Value in Doubly Linked List in DSA C++?
Imagine you have a paper list of your favorite songs, and you want to remove a song you no longer like. You have to find it by reading each song name one by one and then erase it carefully without messing up the order.
Doing this by hand is slow and easy to mess up. You might accidentally skip a song or tear the paper wrong, losing other songs. It takes a lot of time to find the song and remove it safely.
A doubly linked list helps by keeping track of songs with pointers to the previous and next songs. Deleting a song by its name becomes quick and safe because you can jump directly to it and fix the links around it without losing any other songs.
for (int i = 0; i < size; i++) { if (list[i] == value) { // shift all elements left for (int j = i; j < size - 1; j++) { list[j] = list[j + 1]; } size--; break; } }
Node* current = head; while (current != nullptr) { if (current->data == value) { // fix links and delete node break; } current = current->next; }
This lets you quickly and safely remove any item from a list without breaking the order or losing other items.
Think of a music app where you can remove a song from your playlist by its name instantly, without reloading the whole list or losing your place.
Manual removal is slow and error-prone.
Doubly linked lists keep track of neighbors for easy removal.
Deleting by value is faster and safer with this structure.
Practice
Solution
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.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.Final Answer:
The node with the matching value is removed and links are updated. -> Option BQuick Check:
Delete by value removes matching node [OK]
- Not updating both previous and next pointers
- Deleting node without checking value
- Deleting all nodes instead of one
Solution
Step 1: Understand parameter passing for head pointer
Head pointer must be passed by reference to allow modification if the first node is deleted.Step 2: Check function return type
Deletion functions usually return void as they modify the list in place.Final Answer:
void deleteNode(Node* &head, int val) { /* code */ } -> Option AQuick Check:
Pass head by reference and return void [OK]
- Passing head by value causing no list update
- Returning Node instead of void
- Missing head pointer parameter
struct Node {
int data;
Node* prev;
Node* next;
};
// List: 10 <-> 20 <-> 30
// deleteNode(head, 20);
// printList(head);
Solution
Step 1: Identify node to delete
Node with value 20 is in the middle of the list 10 <-> 20 <-> 30.Step 2: Update links after deletion
After deleting 20, 10's next points to 30, and 30's prev points to 10.Final Answer:
10 <-> 30 <-> null -> Option DQuick Check:
Middle node deleted, links updated [OK]
- Not updating previous pointer of next node
- Not updating next pointer of previous node
- Leaving deleted node linked
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;
}Solution
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.Step 2: Verify code for head update
Code does not update head pointer when temp->prev is nullptr (first node).Final Answer:
Does not update head if first node is deleted -> Option CQuick Check:
Head update missing for first node deletion [OK]
- Forgetting to update head pointer
- Not handling empty list
- Deleting without checking node existence
Solution
Step 1: Delete node with value 5 (head node)
Update head to 10, set 10's prev to nullptr. List becomes 10 <-> 15 <-> 20.Step 2: Delete node with value 20 (tail node)
Update 15's next to nullptr. List becomes 10 <-> 15.Final Answer:
10 <-> 15 <-> null -> Option AQuick Check:
Deleted head and tail nodes correctly [OK]
- Not updating head when deleting first node
- Not updating tail when deleting last node
- Leaving dangling pointers
Ready to prove it?
Pass the test and this pattern moves unknown
