1011. Delete all occurrences of a key in DLL

Given the head of a doubly linked list and an integer target. Delete all nodes in the linked list with the value target and return the head of the modified linked list.

Example 1:

Input: head -> 1 <-> 2 <-> 3 <-> 1 <-> 4, target = 1

Output: head -> 2 <-> 3 <-> 4

Explanation: All nodes with the value 1 were removed.

Example 2:

Input: head -> 2 <-> 3 <-> -1 <-> 4 <-> 2, target = 2

Output: head -> 3 <-> -1 <-> 4

Explanation: All nodes with the value 2 were removed.

Note that the value of head is changed.

Now Your Turn!

Pick the correct output for the given input

Input: head -> 7 <-> 7 <-> 7 <-> 7, target = 7

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 <= number of nodes in the linked list <= 105
  • -104 <= ListNode.val <= 104
  • -104 <= target <= 104

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/*
Definition of doubly linked list:
struct ListNode
{
int val;
ListNode *next;
ListNode *prev;
ListNode()
{
val = 0;
next = NULL;
prev = NULL;
}
ListNode(int data1)
{
val = data1;
next = NULL;
prev = NULL;
}
ListNode(int data1, ListNode *next1, ListNode *prev1)
{
val = data1;
next = next1;
prev = prev1;
}
};
*/
 
class Solution {
public:
ListNode * deleteAllOccurrences(ListNode* head, int target) {
 
}
};
Test Case

Input:

Nums
Target