Delete in a Doubly Linked List

easy

You are given the head of a doubly linked list and an integer x. Delete the node at position x (positions are 1-indexed, so the first node is position 1) and return the head of the updated list.

Each node stores an integer in val and holds two pointers: next (to the node after it) and prev (to the node before it). When you remove a node from the middle, its two neighbours must be stitched back together in both directions. When you remove the first node, the new head's prev must become null.

The list is serialized as space-separated values from head to tail. If deleting the only node leaves the list empty, return the empty list.

Hints

You do not need to move any values — deleting a node is about repointing links.
In a doubly linked list the node in front of you already knows who is behind it via prev, so you never need a trailing pointer.
Handle x == 1 on its own: promote head.next to the new head and clear its prev; every other position is the same two-line stitch.

Common doubts

Deleting position 1 has no left neighbour to rewire. Instead you promote head.next to the new head and set its prev to null.
Its next is null, so only the left stitch runs: node.prev.next = null. Guard the node.next.prev write so you do not dereference null.
Yes. Leaving a stale prev means the head still points backward at a deleted node, which quietly breaks any reverse traversal.

Interview follow-ups

Search for the first node whose val matches, then apply the same prev/next splice — the removal logic is identical once you have the node.
An LRU cache pairs a hash map with a doubly linked list; O(1) deletion via this exact splice is what lets it evict and re-insert nodes in constant time.

Fun facts

  • The four-pointer splice you learn here is the beating heart of every LRU cache — the map finds the node, the list deletes it in O(1).
  • Browser back/forward history, text-editor undo stacks, and music playlists are all doubly linked lists using this same delete-and-relink move.

Asked at

AmazonMicrosoftAdobeGoogle
Frequently Sometimes Occasionally
Example 1
Input: list = 1 <-> 3 <-> 2, x = 3
Output: 1 <-> 3
Position 3 holds the value 2. Removing it leaves 1 <-> 3.
Example 2
Input: list = 1 <-> 5 <-> 2 <-> 9, x = 1
Output: 5 <-> 2 <-> 9
Position 1 is the head (value 1). Removing it makes 5 the new head, with its prev set to null.
Constraints

- 1 <= x <= size of the linked list <= 10^6 - 0 <= node.val <= 10^4

Solve this problem →