Bird
Raised Fist0
DSA Javaprogramming~20 mins

Delete Node at Specific Position in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting node at position 3
What is the printed linked list after deleting the node at position 3 (1-based index) from the list?
DSA Java
class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

class LinkedList {
    Node head;

    void deleteAtPosition(int position) {
        if (head == null) return;
        if (position == 1) {
            head = head.next;
            return;
        }
        Node temp = head;
        for (int i = 1; temp != null && i < position - 1; i++) {
            temp = temp.next;
        }
        if (temp == null || temp.next == null) return;
        temp.next = temp.next.next;
    }

    void printList() {
        Node n = head;
        while (n != null) {
            System.out.print(n.data + " -> ");
            n = n.next;
        }
        System.out.print("null\n");
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.head = new Node(10);
        list.head.next = new Node(20);
        list.head.next.next = new Node(30);
        list.head.next.next.next = new Node(40);
        list.head.next.next.next.next = new Node(50);

        list.deleteAtPosition(3);
        list.printList();
    }
}
A10 -> 20 -> 40 -> 50 -> null
B10 -> 20 -> 30 -> 50 -> null
C10 -> 20 -> 30 -> 40 -> null
D20 -> 30 -> 40 -> 50 -> null
Attempts:
2 left
💡 Hint
Remember that deleting at position 3 removes the third node from the start.
❓ Predict Output
intermediate
2:00remaining
Output after deleting node at position 1
What is the printed linked list after deleting the node at position 1 (head node) from the list?
DSA Java
class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

class LinkedList {
    Node head;

    void deleteAtPosition(int position) {
        if (head == null) return;
        if (position == 1) {
            head = head.next;
            return;
        }
        Node temp = head;
        for (int i = 1; temp != null && i < position - 1; i++) {
            temp = temp.next;
        }
        if (temp == null || temp.next == null) return;
        temp.next = temp.next.next;
    }

    void printList() {
        Node n = head;
        while (n != null) {
            System.out.print(n.data + " -> ");
            n = n.next;
        }
        System.out.print("null\n");
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.head = new Node(5);
        list.head.next = new Node(15);
        list.head.next.next = new Node(25);

        list.deleteAtPosition(1);
        list.printList();
    }
}
A15 -> 25 -> null
Bnull
C25 -> null
D5 -> 15 -> 25 -> null
Attempts:
2 left
💡 Hint
Deleting position 1 removes the head node.
🔧 Debug
advanced
2:00remaining
Identify the error in deleteAtPosition method
What error will occur when running this code if position is greater than the length of the list?
DSA Java
void deleteAtPosition(int position) {
    if (head == null) return;
    if (position == 1) {
        head = head.next;
        return;
    }
    Node temp = head;
    for (int i = 1; i < position - 1; i++) {
        temp = temp.next;
    }
    temp.next = temp.next.next;
}
ANo error, deletes correctly
BCompilation error
CArrayIndexOutOfBoundsException
DNullPointerException
Attempts:
2 left
💡 Hint
Think about what happens if temp or temp.next is null.
❓ Predict Output
advanced
2:00remaining
Output after deleting node at last position
What is the printed linked list after deleting the node at the last position from the list?
DSA Java
class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

class LinkedList {
    Node head;

    void deleteAtPosition(int position) {
        if (head == null) return;
        if (position == 1) {
            head = head.next;
            return;
        }
        Node temp = head;
        for (int i = 1; temp != null && i < position - 1; i++) {
            temp = temp.next;
        }
        if (temp == null || temp.next == null) return;
        temp.next = temp.next.next;
    }

    void printList() {
        Node n = head;
        while (n != null) {
            System.out.print(n.data + " -> ");
            n = n.next;
        }
        System.out.print("null\n");
    }

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

        list.deleteAtPosition(3);
        list.printList();
    }
}
A2 -> 3 -> null
B1 -> 3 -> null
C1 -> 2 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting last node removes the tail.
🧠 Conceptual
expert
2:00remaining
Effect of deleting node at invalid position
If you try to delete a node at position 10 in a linked list of length 5 using the given deleteAtPosition method, what will be the state of the linked list after the operation?
DSA Java
void deleteAtPosition(int position) {
    if (head == null) return;
    if (position == 1) {
        head = head.next;
        return;
    }
    Node temp = head;
    for (int i = 1; temp != null && i < position - 1; i++) {
        temp = temp.next;
    }
    if (temp == null || temp.next == null) return;
    temp.next = temp.next.next;
}
AThe head node is deleted
BThe list remains unchanged
CThe last node is deleted
DThe list becomes empty
Attempts:
2 left
💡 Hint
Check the condition that prevents deletion if position is invalid.

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