Removing given node in DLL

Linked-List Fundamentals (Doubly LL) Easy

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.

Examples:

Input: head -> 1 <-> 3 <-> 5, node = 3

Output: head -> 1 <-> 5

Explanation: The referenced node with value 3 was removed.

Input: head -> 1 <-> 3 <-> 7, node = 7

Output: head -> 1 <-> 3

Explanation: The referenced node with value 7 was removed.

Input: head -> 1 <-> 5, node = 5

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

  • Update the previous node’s next pointer to skip the given node. If the given node has a next node, update the next node’s prev pointer to skip the given node.
  • If the given node is the tail (next == None), simply update prev.next = None. Ensure all pointers are updated correctly to avoid memory leaks or dangling references.

Company Tags

Databricks Western Digital Epic Systems Byju's Bloomberg PwC Unity Technologies Mastercard Medtronic Airbnb Electronic Arts Epic Games eBay Riot Games Bungie Johnson & Johnson Rakuten Goldman Sachs Teladoc Health Swiggy Twilio Deloitte Bain & Company Roche Philips Healthcare TCS Cognizant Accenture Infosys Capgemini Wipro