Delete all occurrences of a Key in DLL

Medium Problems of Doubly Linked List DSA practice problem on Onlearn.

Difficulty: medium.

Topics: How to delete all occurrences of a key in a doubly linked list (DLL), Doubly Linked List, Linked List, Node, Pointer, Algorithm, edge cases, node operations, pointer operations, linked list, traversal, Linked List Operations.

Delete All Occurrences of a Key in a Doubly Linked List Problem Statement Given the head of a Doubly Linked List and an integer key, your task is to delete all nodes in the list whose val attribute is equal to key. The resulting list should maintain the properties of a Doubly Linked List. Doubly Linked List Node Definition Input Specification The input will consist of: 1. The head of a Doubly Linked List. 2. An integer key representing the value to be deleted. Output Specification Return the head of the modified Doubly Linked List after all occurrences of key have been deleted. If the list becomes empty, return nullptr. Constraints The number of nodes in the list N will be between 0 and 10^5. The value of nodes Node.val and key will be between 10^9 and 10^9. Sample Test Cases Sample 1: Input: Output: Explanation: All nodes with value 2 are removed, resulting in a list containing 1, 3, and 4. Sample 2: Input: Output: Explanation: All nodes are 7, so the entire list is deleted, resulting in an empty list. Sample 3: Input: Output: Explanation: The key 5 is not present in the list, so the list remains unchanged.