333. Doubly Linked List — Delete a Node

EasyLinked ListDoubly Linked ListDLLDeletion

Given the head of a doubly linked list and an integer val, delete the first node whose value equals val. Correctly update both prev and next pointers. Return the head.

If val is not found, return the list unchanged.

Input: Head of a DLL and integer val.

Output: Head of the updated DLL.

Examples

Example 1
Input: [1,2,3,4,5], 3
Output: [1,2,4,5]
Explanation: Find node(3). node(2).next=node(4); node(4).prev=node(2). Unlinked node(3).
Example 2
Input: [1,2,3], 1
Output: [2,3]
Explanation: Delete head. New head=node(2); node(2).prev=NULL.
Example 3
Input: [1,2,3], 4
Output: [1,2,3]
Explanation: Val 4 not found. Return unchanged.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →