Delete all Occurrences of a Key in Doubly Linked List
Problem Statement:β
You are given theΒ head_refΒ of a doubly Linked List and aΒ Key. Your task is toΒ delete all occurrencesΒ of the given key if it is present and return the new DLL.
-
Example:
Example1:
Input:
2<->2<->10<->8<->4<->2<->5<->2
2
Output:
10<->8<->4<->5
Explanation:
All Occurences of 2 have been deleted.
Example2:
Input:
9<->1<->3<->4<->5<->1<->8<->4
9
Output:
1<->3<->4<->5<->1<->8<->4
Explanation:
All Occurences of 9 have been deleted.
β Solution: Doubly Linked List Node Deletionβ
class Solution {
public:
void deleteAllOccurOfX(Node** head_ref, int targetValue) {
Node* current = *head_ref;
while (current != NULL) {
Node* nextNode = current->next;
if (current->data == targetValue) {
// If current node is the head
if (current->prev == NULL) {
*head_ref = current->next;
if (*head_ref) (*head_ref)->prev = NULL;
}
// If current node is not the head
else {
current->prev->next = current->next;
if (current->next)
current->next->prev = current->prev;
}
// Optional: Free memory if needed
// delete current;
}
current = nextNode; // Move to next node
}
}
};
π How It Worksβ
- Traverse the doubly linked list using a pointer
current. - For each node:
- If its value matches
targetValue, adjust thenextandprevpointers of neighboring nodes to bypass the current node. - If itβs the head node, update
head_refto point to the new head.
- If its value matches
- Move to the next node before modifying the current one (store
nextbeforehand).
β±οΈ Time & Space Complexityβ
| Metric | Value |
|---|---|
| Time | O(N) |
| Space | O(1) |
β οΈ Edge Cases Handledβ
- Empty list (
head == NULL) - All nodes are equal to
x - First node (head) has value
x - Last node has value
x - Multiple
xvalues scattered in the list
π‘ Other Approachesβ
- For a singly linked list, a dummy head pointer is often used to simplify head deletions.
- Can be extended to return count of deleted nodes or a boolean if deletion occurred.
π Related Problemsβ
- Delete Node in a Doubly Linked List
- Remove Duplicates in a Linked List
- Delete Middle Node in Linked List
- Delete Nth Node from End of Linked List
π¬