A2Z Lesson

Log in
← Back to the A2Z sheet

Step 08 Linked-List — Fundamentals (Doubly LL)

Insertion in DLL

Medium

Inserting into a doubly linked list means creating a node and wiring four links consistently: the new node's prev and next, plus one link on each neighbour. This lesson covers insertion before the head, before the tail, before the k-th node and before a given node — the four forms the practice problems take — and the update order that keeps both directions valid at every step.

The four-link rule

Splicing a new node N between P (predecessor) and S (successor) requires: N.prev = P, N.next = S, P.next = N, S.prev = N. Links touching the new node can be set in any order since N is not yet reachable, but the two neighbour links (P.next and S.prev) should be set after N's own links are ready, and neither neighbour link may be skipped.

Most DLL problems phrase insertion as 'before X' rather than 'after X' — natural for a doubly list because the node you are given can reach its predecessor through prev.

Case 1: insert before head, Case 2: insert before tail

Before head: N.next = head, N.prev = None, head.prev = N, and N becomes the new head. This also handles the empty list (head is None, so just return N).

Before tail: walk to the tail T. Its predecessor is T.prev. Splice N between T.prev and T with the four-link rule. If the list has one node, the tail is the head and this degenerates to Case 1.

def insert_before_head(head, x): node = DNode(x) if head is None: return node node.next = head head.prev = node return node def insert_before_tail(head, x): if head is None or head.next is None: return insert_before_head(head, x) tail = head while tail.next is not None: tail = tail.next prev_node = tail.prev node = DNode(x, prev=prev_node, next=tail) prev_node.next = node tail.prev = node return head

Case 3: insert before the k-th node, Case 4: insert before a given node

Before the k-th node (1-indexed): if k == 1 use the head routine. Otherwise walk to the k-th node T and splice before it using T.prev — identical mechanics to Case 2 with a different target. If k exceeds the length, the target does not exist; leave the list unchanged (or append, per the problem statement).

Before a given node: you are handed T directly, so no walk — read P = T.prev and apply the four-link rule; if P is None, T was the head and the new node becomes head.

def insert_before_kth(head, x, k): if k == 1 or head is None: return insert_before_head(head, x) temp = head count = 1 while temp is not None and count < k: temp = temp.next count += 1 if temp is None: # k out of range return head prev_node = temp.prev node = DNode(x, prev=prev_node, next=temp) prev_node.next = node temp.prev = node return head def insert_before_node(node, x): # returns the (possibly new) head; node must not be None prev_node = node.prev new_node = DNode(x, prev=prev_node, next=node) node.prev = new_node if prev_node is None: return new_node # inserted before the old head prev_node.next = new_node return None # head unchanged; caller keeps old head

Complexity and common mistakes

Rewiring is O(1) in all cases; total cost is O(n) when a walk is needed (tail, k-th) and O(1) for before-head and before-given-node. Space is O(1) beyond the new node.

Common mistakes: (1) setting only three of the four links — usually the successor's prev — so backward traversal skips or resurrects nodes; (2) in before-head insertion, forgetting head.prev = node; (3) in the k-th case, splicing after the k-th node instead of before it (read the problem statement twice — 'insert at position k' and 'insert before the k-th node' differ by one); (4) losing the new head: before-head insertion changes head, so the function must return it and callers must keep it.

Key points

  • A DLL splice touches four links: new node's prev/next and one link on each neighbour.
  • Insert before head is O(1) and produces a new head with prev = None.
  • Before-tail and before-kth differ only in how you reach the target node; the splice is identical.
  • Before a given node is O(1) — the node itself supplies its predecessor via prev.
  • Skipping the successor's prev update is the classic silent corruption.

Suggested questions

Insert node before head in Doubly Linked List ↗ Case 1 — new head, and remember the old head's prev.
Insert node before tail in Doubly Linked List ↗ Case 2 — walk to the tail, splice before it.
Insert node before (kth node) in Doubly Linked List ↗ Case 3 — same splice, positional target.
Insert before given node in Doubly Linked List ↗ Case 4 — the O(1) version with the node handed to you.

Source note: this lesson follows the structure of Striver's public A2Z DSA course (takeUforward). Each problem links to its original platform, and all problems and content belong to their respective owners — STUDY-BUDDY does not claim ownership of the sheet or its problems, and no permission beyond public linking is claimed.