Delete a Node at a Given Position in a Linked List

Solve this Problem
Medium10–15 min
Topics
Companies
Practice:GFG ↗
Given the head of a singly linked list and a 0-indexed position, delete the node at that index and return the head of the resulting list. position = 0 is exactly the delete-at-the-beginning case. For any other position, the node being deleted is never visited directly — only the node right before it matters, since deleting means redirecting that node's next pointer straight past the target.

Test Case 1:

Input:head = [1, 2, 3, 4, 5], position = 2
Output:[1, 2, 4, 5]
Explanation:The node at index 2 (value 3) is removed.

Test Case 2:

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

Test Case 3:

Input:head = [10, 20, 30], position = 0
Output:[20, 30]
Explanation:position 0 is equivalent to deleting the head.

Constraints

  • 1 ≤ number of nodes in head ≤ 10⁴
  • -10⁹ ≤ node value ≤ 10⁹
  • 0 ≤ position < length of head — position is 0-indexed, so position 0 deletes the head
🚀

Try the Dry Run

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

🧪Try your own test case
1class Solution {
2 public Node deleteAtPosition(Node head, int position) {
3 if (position == 0) {
4 return head.next;
5 }
6 Node curr = head;
7 for (int i = 0; i < position - 1; i++) {
8 curr = curr.next;
9 }
10 curr.next = curr.next.next;
11 return head;
12 }
13}
14
1
2
3
4
5
null
Variables
position2
COMPARE

position is 2, not 0 — walk to the node just before the target index instead.

Step 1 / 5

Approach & Solutions

Brute Force — Copy to Array Except That Index, Rebuild List

Brute

Walk the entire list into a plain array, then remove the element at the given position, shifting everything after it one slot left. Throw the original list away and build a brand-new list from that array using a dummy + tail pointer. Correct, but every node — not just the one being deleted — is read and re-allocated.

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 deleteAtPosition(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.

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 deleteAtPosition(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