820. Removing given node in Doubly Linked List

Given a node's reference within a doubly linked list, remove that node from the linked list while preserving the list's integrity.

You will only be given the node's reference, not the head of the list. It is guaranteed that the given node will not be the head of the list.

Example 1:

Input: head = [1, 3, 5], node = 3

Output: [1, 5]

Explanation:

  • The referenced node with value 3 was removed.

Example 2:

Input: head = [1, 3, 7], node = 7

Output: head = [1, 3]

Explanation:

  • The referenced node with value 7 was removed.

Now Your Turn!

Pick the correct output for the given input

Input: head = [1, 5], node = 5

Still unsure what the problem is asking ?

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

Constraints:

  • 2 <= Number of nodes in the list <= 100
  • 0 <= ListNode.val <= 100
  • Node is guaranteed to be a part of the linked list and will not be the head

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/**
class ListNode
{
* Definition for doubly-linked list.
* public:
* int data;
* ListNode *prev;
* ListNode *next;
* ListNode() : data(0), prev(nullptr), next(nullptr) {}
* ListNode(int x) : data(x), prev(nullptr), next(nullptr) {}
* ListNode(int x, ListNode *prev, ListNode *next) : data(x), prev(prev), next(next) {}
};
*/
 
class Solution {
public:
void deleteGivenNode(ListNode *node) {
// Your code goes here
}
};
Test Case

Input:

Node
Nums