Insert node before tail in Doubly Linked List
Pattern: Doubly and circular lists
Walk to the tail and link the new node between the tail's prev and the tail, four pointers in all. With one node, inserting before the tail means inserting before the head.
The problem
Given the head of a non-empty doubly linked list and a value X, insert a new node holding X just before the last
node, and return the head. For example, 1 ⇄ 2 ⇄ 4 with X = 3 becomes 1 ⇄ 2 ⇄ 3 ⇄ 4. With one node, the new node
becomes the head.
The function name insertBeforeTail 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.
Think first
- Which two nodes will the new node sit between?
- How many pointers change?
- What if the tail is also the head?
Hints
Brute force, and why it fails
Rebuilding is O(n) with new nodes. One walk and four links is O(n) with one new node.
The key insight
Inserting between two known nodes is always the same four links. The only special case is when one of the two is missing.
Dry run
The list is 1 ⇄ 2 ⇄ 4 and X = 3. Walk to the tail, 4. The node before it is 2.
The plan
if head.next is null: node.next = head; head.prev = node; return node
tail = last node; before = tail.prev
node = new DNode(X)
before.next = node; node.prev = before
node.next = tail; tail.prev = node
return head
Complexity
- Time
O(n)- Space
O(1)
One walk to the tail.
Edge cases
- One node: the new node becomes the head.
- Two nodes: the new node goes between them.
- Long lists: the walk is the whole cost.
I've built my logic, show the code
Java
class Solution {
public DNode insertBeforeTail(DNode head, int X) {
if (head.next == null) { // one node: inserting before it means a new head
DNode node = new DNode(X);
node.next = head;
head.prev = node;
return node;
}
DNode tail = head;
while (tail.next != null) tail = tail.next;
DNode node = new DNode(X), before = tail.prev;
before.next = node; node.prev = before; // before <-> node
node.next = tail; tail.prev = node; // node <-> tail
return head;
}
}C++
class Solution {
public:
DNode* insertBeforeTail(DNode* head, int X) {
if (head->next == NULL) { // one node: inserting before it means a new head
DNode* node = new DNode(X);
node->next = head;
head->prev = node;
return node;
}
DNode* tail = head;
while (tail->next != NULL) tail = tail->next;
DNode* node = new DNode(X);
DNode* before = tail->prev;
before->next = node; node->prev = before; // before <-> node
node->next = tail; tail->prev = node; // node <-> tail
return head;
}
};Python
from lib import DNode
class Solution:
def insertBeforeTail(self, head: DNode, X: int) -> DNode:
if head.next is None: # one node: inserting before it means a new head
node = DNode(X, None, head)
head.prev = node
return node
tail = head
while tail.next is not None:
tail = tail.next
before = tail.prev
node = DNode(X, before, tail) # before <-> node <-> tail
before.next = node
tail.prev = node
return headCommon mistakes
- Inserting after the tail instead of before.
- Forgetting one of the four links.
- Ignoring the one-node case, where
tail.previs null.