Delete All Occurrences of a Key in a Doubly Linked List

Implement deleteAllKeyDLL

Given the head of a doubly linked list and an integer key, delete every node whose value equals key — the key may appear any number of times, anywhere in the list — and return the head of the resulting list. This is a bypass problem, not a value-overwrite problem: a matching node is removed by redirecting the pointer in front of it to skip over it, not by copying values around. In a true doubly linked list, deleting a node also means pointing the node right after it back at the node right before it, so both directions of the chain stay consistent.

Example 1:

Input: head = [4,2,4,6,4,3], key = 4

Output: [2,6,3]

Example 2:

Input: head = [1,1,1], key = 1

Output: []

Example 3:

Input: head = [1,2,3], key = 9

Output: [1,2,3]

+ 4 hidden test cases run on Submit.

Constraints:

  • 0 ≤ number of nodes in head ≤ 200
  • -1000 ≤ node value ≤ 1000
  • key may appear zero, one, or many times in the list

head =

[4, 2, 4, 6, 4, 3]

key =

4