Delete a Node in a Doubly Linked List

Solve this Problem
Easy10–15 min
Topics
Companies
Practice:GFG ↗
Given the head of a doubly linked list and a 0-indexed position, delete the node at that position and return the new head. Position 0 means deleting the head itself. Removing a node from the middle only ever touches the pointers of its immediate neighbors — nothing else in the list changes. In a singly linked list, that means redirecting one next pointer to skip past the removed node; in a doubly linked list, the node right after it also has its prev pointer redirected back to the one before, so the shortened list still reads correctly in both directions.

Test Case 1:

Input:head = [2, 5, 6, 7, 9], position = 2
Output:[2, 5, 7, 9]
Explanation:The node at index 2 (value 6) is bypassed — its neighbors (5 and 7) are relinked directly to each other.

Test Case 2:

Input:head = [1, 2, 3], position = 0
Output:[2, 3]
Explanation:position 0 makes this equivalent to deleting the head.

Test Case 3:

Input:head = [5], position = 0
Output:[]
Explanation:Deleting the only node leaves an empty list.

Constraints

  • 1 ≤ number of nodes in head ≤ 200
  • -1000 ≤ node value ≤ 1000
  • 0 ≤ position < length of head — position is 0-indexed
  • This platform's judge reads back only the forward (next) traversal — a real doubly linked implementation also relinks the removed node's neighbors' .prev pointers
🚀

Try the Dry Run

Don't just read the solution — watch it execute, one step at a time.

Approach & Solutions

Brute Force — Copy to Array, Remove, Rebuild

Brute

Copy every value into an array, remove the entry at the target position, then rebuild the list from that shortened array. Correct, but it discards and reallocates every remaining node instead of relinking the two pointers around the deleted node.

TimeO(n)
SpaceO(n)
1// Node definition used in this problem: 2// class Node { 3// int val; 4// Node next; 5// } 6 7class Solution { 8 public Node deleteDLL(Node head, int position) { 9 List<Integer> vals = new ArrayList<>(); 10 Node curr = head; 11 while (curr != null) { 12 vals.add(curr.val); 13 curr = curr.next; 14 } 15 vals.remove(position); 16 Node dummy = new Node(0); 17 Node tail = dummy; 18 for (int v : vals) { 19 tail.next = new Node(v); 20 tail = tail.next; 21 } 22 return dummy.next; 23 } 24}

Optimal — Traverse to Position-1, Bypass the Target Node

Optimal

Position 0 is really just delete-at-the-beginning — handle it the same O(1) way. Otherwise, walk to the node just before the target position, then redirect its next pointer straight past the target node: curr.next = curr.next.next. The target node is never visited or modified — it's simply skipped over. In a true doubly linked list, the node right after the deleted one also has its prev pointed back at curr, the same bypass mirrored backward.

TimeO(n)
SpaceO(1) extra
1// Node definition used in this problem: 2// class Node { 3// int val; 4// Node next; 5// } 6 7class Solution { 8 public Node deleteDLL(Node head, int position) { 9 if (position == 0) { 10 return head.next; 11 } 12 Node curr = head; 13 for (int i = 0; i < position - 1; i++) { 14 curr = curr.next; 15 } 16 curr.next = curr.next.next; 17 return head; 18 } 19}

Related Problems