Insert before given node in Doubly Linked List

Pattern: Doubly and circular lists

medium

You are handed a node that is not the head. Its prev is reachable, so make a new node between prev and the given node with four links.

The problem

You are given one node of a doubly linked list, which is not the head, and a value X. Insert a new node holding X just before the given node; the function returns nothing. For example, handed the node 2 in 1 ⇄ 2 ⇄ 3 with X = 5, the list becomes 1 ⇄ 5 ⇄ 2 ⇄ 3.

The function name insertBeforeGivenNode matches the takeUforward practice page. Use the link above for the exact statement and limits. In this guide's code the node type is DNode. The tests hand the function the node at a given index and print the list from the head afterwards.

Think first

  • Why does "not the head" matter?
  • Which nodes does the new node sit between?
  • In which order do you change the four pointers?

Hints

    Brute force, and why it fails

    Nothing is simpler: four links, O(1).

    The key insight

    With prev, "before this node" is as easy as "after this node". No walk from the head is needed.

    Dry run

    10 
    21 
    32 

    The list is 1 ⇄ 2 ⇄ 3, you are handed the node 2, and X = 5.

    Step 1 of 3

    The plan

    before = node.prev
    fresh = new DNode(X); fresh.prev = before; fresh.next = node
    before.next = fresh
    node.prev = fresh
    

    Complexity

    Time
    O(1)
    Space
    O(1)

    Four assignments and one new node.

    Edge cases

    • The node is the tail: the new node goes second to last.
    • The node is second: the new node goes right after the head.
    • Two nodes: three after the insertion.
    I've built my logic, show the code

    Java

    class Solution {
        public void insertBeforeGivenNode(DNode node, int X) {
            DNode before = node.prev;               // never null: the node is not the head
            DNode fresh = new DNode(X);
            fresh.prev = before;
            fresh.next = node;
            before.next = fresh;
            node.prev = fresh;
        }
    }

    C++

    class Solution {
    public:
        void insertBeforeGivenNode(DNode* node, int X) {
            DNode* before = node->prev;             // never NULL: the node is not the head
            DNode* fresh = new DNode(X);
            fresh->prev = before;
            fresh->next = node;
            before->next = fresh;
            node->prev = fresh;
        }
    };

    Python

    from lib import DNode
    
    class Solution:
        def insertBeforeGivenNode(self, node: DNode, X: int) -> None:
            before = node.prev                      # never None: the node is not the head
            fresh = DNode(X, before, node)
            before.next = fresh
            node.prev = fresh

    Common mistakes

    • Setting node.prev = fresh before saving the old prev.
    • Forgetting before.next, so walking forwards skips the new node.
    • Walking from somewhere to find the node before, which is not needed.

    Similar problems