Bird
Raised Fist0
DSA Javaprogramming

Delete Node at Specific Position in DSA Java

Choose your learning style10 modes available
Mental Model
To remove a node from a linked list, find the node just before it and change its pointer to skip the node to delete.
Analogy: Imagine a chain of paper clips linked together. To remove one clip in the middle, you unhook the clip before it and link it directly to the clip after the one you want to remove.
head -> 1 -> 2 -> 3 -> 4 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> 4 -> null, delete node at position 3
Goal: Remove the node at position 3 (value 3) and keep the list connected
Step 1: Check if position is 1 (head), it is not, so proceed
head -> 1 -> 2 -> 3 -> 4 -> null
Why: We only change head if deleting first node
Step 2: Move to node before position 3, which is node 2
head -> 1 -> [curr->2] -> 3 -> 4 -> null
Why: We need the node before the one to delete to change its pointer
Step 3: Change curr.next to skip node 3 and point to node 4
head -> 1 -> 2 -> 4 -> null
Why: Skipping node 3 removes it from the list
Result:
head -> 1 -> 2 -> 4 -> null
Annotated Code
DSA Java
class Node {
    int data;
    Node next;
    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class DeleteNodeAtPosition {
    static Node deleteAtPosition(Node head, int position) {
        if (head == null) return null; // empty list
        if (position == 1) {
            return head.next; // remove head
        }
        Node curr = head;
        for (int i = 1; i < position - 1; i++) {
            if (curr.next == null) return head; // position out of bounds
            curr = curr.next; // move to node before target
        }
        if (curr.next != null) {
            curr.next = curr.next.next; // skip target node
        }
        return head;
    }

    static void printList(Node head) {
        Node curr = head;
        while (curr != null) {
            System.out.print(curr.data);
            if (curr.next != null) System.out.print(" -> ");
            curr = curr.next;
        }
        System.out.println(" -> null");
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);

        System.out.print("Original list: ");
        printList(head);

        int position = 3;
        head = deleteAtPosition(head, position);

        System.out.print("After deleting position " + position + ": ");
        printList(head);
    }
}
if (head == null) return null; // empty list
handle empty list edge case
if (position == 1) { return head.next; // remove head }
remove first node by returning second node as new head
for (int i = 1; i < position - 1; i++) { if (curr.next == null) return head; // position out of bounds curr = curr.next; // move to node before target }
move to node before the one to delete, stop if position invalid
if (curr.next != null) { curr.next = curr.next.next; // skip target node }
skip the node at position by linking previous node to next of target
OutputSuccess
Original list: 1 -> 2 -> 3 -> 4 -> null After deleting position 3: 1 -> 2 -> 4 -> null
Complexity Analysis
Time: O(n) because we may need to traverse up to n nodes to reach the position
Space: O(1) because we only use a few pointers and no extra data structures
vs Alternative: Compared to rebuilding the list or using arrays, this is efficient as it changes pointers directly without copying
Edge Cases
empty list
returns null without error
DSA Java
if (head == null) return null; // empty list
delete head node (position 1)
head moves to next node
DSA Java
if (position == 1) {
    return head.next; // remove head
}
position out of bounds (greater than list length)
list remains unchanged
DSA Java
if (curr.next == null) return head; // position out of bounds
When to Use This Pattern
When you need to remove an element from a linked list at a given position, use this pattern to adjust pointers and keep the list connected.
Common Mistakes
Mistake: Not handling deletion of the head node separately
Fix: Add a check for position 1 and return head.next to update the head
Mistake: Not checking if position is out of bounds causing null pointer errors
Fix: Check if curr.next is null before moving forward and return original head if position invalid
Mistake: Changing the pointer of the node to delete instead of the previous node
Fix: Always change the next pointer of the node before the one to delete
Summary
Deletes a node at a specific position by changing pointers to skip it.
Use when you want to remove an element from anywhere in a linked list by position.
The key is to find the node before the target and link it to the node after the target.

Practice

(1/5)
1. What happens when you delete a node at position 1 in a singly linked list?
easy
A. The list remains unchanged
B. The last node is removed
C. The head node is removed and the next node becomes the new head
D. All nodes are deleted

Solution

  1. Step 1: Understand position 1 in linked list

    Position 1 refers to the head node of the list.
  2. Step 2: Delete the head node

    Removing the head means the next node becomes the new head.
  3. Final Answer:

    The head node is removed and the next node becomes the new head -> Option C
  4. Quick Check:

    Deleting position 1 removes head [OK]
Hint: Deleting position 1 means removing the head node [OK]
Common Mistakes:
  • Thinking position 1 means last node
  • Assuming list stays same after deletion
  • Deleting all nodes instead of one
2. Which of the following is the correct Java syntax to delete a node at position pos in a singly linked list starting with head?
easy
A. if (pos == 1) head = head.next;
B. head = head.next.next;
C. head.next = null;
D. head = null;

Solution

  1. Step 1: Check deletion at position 1

    If position is 1, we remove the head by pointing head to head.next.
  2. Step 2: Validate syntax correctness

    if (pos == 1) head = head.next; correctly assigns head = head.next; others either skip nodes or nullify incorrectly.
  3. Final Answer:

    if (pos == 1) head = head.next; -> Option A
  4. Quick Check:

    Correct syntax for deleting head node [OK]
Hint: Deleting head means head = head.next [OK]
Common Mistakes:
  • Skipping two nodes instead of one
  • Setting head.next to null incorrectly
  • Setting entire head to null without condition
3. Given the linked list: 10 -> 20 -> 30 -> 40 -> null, what is the list after deleting the node at position 3?
medium
A. 10 -> 20 -> 40 -> null
B. 10 -> 30 -> 40 -> null
C. 20 -> 30 -> 40 -> null
D. 10 -> 20 -> 30 -> null

Solution

  1. Step 1: Identify node at position 3

    Position 3 node is 30 in the list 10 -> 20 -> 30 -> 40 -> null.
  2. Step 2: Remove node 30 and link previous to next

    Link node 20's next to node 40, skipping 30.
  3. Final Answer:

    10 -> 20 -> 40 -> null -> Option A
  4. Quick Check:

    Deleted 3rd node 30 correctly [OK]
Hint: Skip the node at position by linking previous to next [OK]
Common Mistakes:
  • Deleting wrong node position
  • Not updating previous node's next pointer
  • Removing head instead of position 3
4. Consider this code snippet to delete a node at position pos in a singly linked list:
Node temp = head;
for (int i = 1; i < pos - 1; i++) {
    temp = temp.next;
}
temp.next = temp.next.next;

What error can occur if pos is 1?
medium
A. ArrayIndexOutOfBoundsException
B. NullPointerException because temp.next is null
C. The head node is deleted correctly
D. No error, deletion works fine

Solution

  1. Step 1: Analyze loop for pos = 1

    Loop runs from i=1 to pos-2 = -1, so it does not run; temp = head.
  2. Step 2: Access temp.next when pos=1

    temp.next is head.next, but deletion logic expects previous node; deleting head this way causes NullPointerException.
  3. Final Answer:

    NullPointerException because temp.next is null -> Option B
  4. Quick Check:

    Deleting position 1 needs special handling [OK]
Hint: Deleting head needs separate code; loop fails for pos=1 [OK]
Common Mistakes:
  • Assuming loop runs for pos=1
  • Not handling head deletion separately
  • Expecting no error for pos=1
5. You have a singly linked list: 5 -> 10 -> 15 -> 20 -> 25 -> null. Write the resulting list after deleting the node at position 5, then position 1, and finally position 2 in that order.
hard
A. 10 -> 20 -> 25 -> null
B. 10 -> 15 -> 25 -> null
C. 5 -> 15 -> 20 -> 25 -> null
D. 10 -> 20 -> null

Solution

  1. Step 1: Delete node at position 5

    Position 5 node is 25; after deletion list: 5 -> 10 -> 15 -> 20 -> null.
  2. Step 2: Delete node at position 1

    Delete head (5); new list: 10 -> 15 -> 20 -> null.
  3. Step 3: Delete node at position 2

    Position 2 node is 15; after deletion list: 10 -> 20 -> null.
  4. Final Answer:

    10 -> 20 -> null -> Option D
  5. Quick Check:

    Sequential deletions update list correctly [OK]
Hint: Delete from end, then head, then middle stepwise [OK]
Common Mistakes:
  • Deleting wrong nodes after list changes
  • Not updating head after first deletion
  • Confusing positions after deletions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard