A2Z Lesson

Log in
← Back to the A2Z sheet

Step 08 Linked-List — Fundamentals (Doubly LL)

Deletion in Doubly LL

Medium

Deleting from a doubly linked list follows the same skip-over idea as the singly case, but every deletion must repair links in both directions: the predecessor's next and the successor's prev. This lesson covers deleting the head, the tail, the k-th node and a given node, with the invariant checks that keep the backward chain intact.

The two-direction splice

To remove node X with predecessor P and successor N: set P.next = N and N.prev = P. Either link may be absent — if X is the head, there is no P, and the answer is a new head whose prev must be None; if X is the tail, there is no N. If X is the only node, the list becomes empty.

Write the boundary cases first (empty list, single node), then the general splice handles everything else uniformly.

Case 1: delete head and Case 2: delete tail

Head: if head is None return None; if head.next is None return None (single node). Otherwise new_head = head.next and new_head.prev = None — forgetting that prev reset is the number-one DLL bug, because the new head still points back at the deleted node.

Tail: walk to the tail (or use tail.prev if you already hold the tail), then tail.prev.next = None. Unlike the singly list, no second-last hunt is needed once you are at the tail — prev gives it to you in O(1).

def delete_head_dll(head): if head is None or head.next is None: return None new_head = head.next new_head.prev = None # do not forget this return new_head def delete_tail_dll(head): if head is None or head.next is None: return None tail = head while tail.next is not None: tail = tail.next tail.prev.next = None return head

Case 3: delete the k-th node

Walk a temp pointer to position k with a counter. If k == 1 it is the head case; if temp.next is None it is the tail case; in the middle, splice both directions: temp.prev.next = temp.next and temp.next.prev = temp.prev. Because the node carries its own prev, you do not need the separate prev-tracking variable the singly version required — the node knows its predecessor.

def delete_kth_dll(head, k): if head is None: return None 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, next_node = temp.prev, temp.next if prev_node is None and next_node is None: return None # only node if prev_node is None: next_node.prev = None return next_node # new head if next_node is None: prev_node.next = None return head # new tail prev_node.next = next_node next_node.prev = prev_node return head

Case 4: delete a given node (not the head)

Some problems hand you the node itself and promise it is not the head. Then deletion is pure O(1): prev_node.next = node.next, and if node.next exists, node.next.prev = prev_node. No walk at all — this is the payoff for the extra pointer.

Complexity for the other cases is dominated by the walk: O(n) time, O(1) space. Common mistakes: resetting the wrong prev after head deletion; in the k-th case, treating 'k == length' with the middle splice (its next_node is None and next_node.prev would crash — the boundary branches above exist precisely for this); and leaving the deleted node's own pointers dangling into the list, which matters in C++ where you also free the node.

Key points

  • Every DLL deletion repairs two links: predecessor's next and successor's prev.
  • After deleting the head, set the new head's prev to None — the most common DLL bug is skipping this.
  • Deleting the tail or a given node is O(1) once the node is in hand, thanks to prev.
  • Delete-kth unifies the others: walk, then branch on whether prev/next are None.
  • Boundary cases first: empty list and single-node list both delete to None.

Suggested questions

Delete head of Doubly Linked List ↗ Case 1 — including the prev reset on the new head.
Delete Tail of Doubly Linked List ↗ Case 2 — tail.prev makes this O(1) after the walk.
Delete Kth Element of Doubly Linked List ↗ Case 3 — one routine that must handle head, middle and tail uniformly.
Removing given node in Doubly Linked List ↗ Case 4 — pure O(1) splice with no traversal.

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.