Insert before given node in DLL

Linked-List Fundamentals (Doubly LL) Easy

Given a node's reference within a doubly linked list and an integer X, insert a node with value X before the given node in 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 <-> 2 <-> 6, node = 2, X = 7

Output: head -> 1 <-> 7 <-> 2 <-> 6

Explanation: Note that the head was not given to the function.

Input: head -> 7 <-> 5 <-> 5, node = 5 (node 3), X = 10

Output: head -> 7 <-> 5 <-> 10 <-> 5

Explanation: The last node with value 5 was referenced, thus the new node was added before the last node.

Input: head -> 7 <-> 6 <-> 5, node = 5, X = 10

Constraints

  • n == Number of nodes in the Linked List
  • 2 <= n <= 100
  • 0 <= ListNode.val <= 100
  • 0 <= X <= 100
  • It is guaranteed the given node will be a part of a doubly linked list and will not be its head.

Hints

  • Create a new node with value X. Find the previous node (prev_node) of given_node using given_node->prev.
  • Update the pointers: prev_node->next = new_node new_node->prev = prev_node new_node->next = given_node given_node->prev = new_node

Company Tags

Zomato Deloitte OYO Rooms DoorDash Pinterest Epic Systems Electronic Arts Walmart Goldman Sachs Target Bain & Company Airbnb Johnson & Johnson Robinhood Intel HashiCorp Dropbox Instacart Swiggy Rakuten Chewy IBM Boston Consulting Group Unity Technologies Ubisoft TCS Cognizant Accenture Infosys Capgemini Wipro