Bird
Raised Fist0
DSA Javaprogramming~20 mins

Insert at Middle Specific Position in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Insert at Middle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after inserting at position 3 in a linked list
What is the printed linked list after inserting value 99 at position 3?
DSA Java
class Node {
    int data;
    Node next;
    Node(int data) { this.data = data; this.next = null; }
}

class LinkedList {
    Node head;

    void insertAtPosition(int data, int position) {
        Node newNode = new Node(data);
        if (position == 1) {
            newNode.next = head;
            head = newNode;
            return;
        }
        Node current = head;
        for (int i = 1; i < position - 1 && current != null; i++) {
            current = current.next;
        }
        if (current == null) return;
        newNode.next = current.next;
        current.next = newNode;
    }

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

public class Main {
    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.insertAtPosition(99, 3);
        list.printList();
    }
}
A99 -> 10 -> 20 -> 30 -> 40 -> null
B10 -> 20 -> 30 -> 40 -> 99 -> null
C10 -> 20 -> 30 -> 99 -> 40 -> null
D10 -> 20 -> 99 -> 30 -> 40 -> null
Attempts:
2 left
💡 Hint
Remember, position 3 means the new node should be the third node in the list.
❓ Predict Output
intermediate
2:00remaining
Result after inserting at position 1 (head) in a linked list
What is the printed linked list after inserting value 5 at position 1?
DSA Java
class Node {
    int data;
    Node next;
    Node(int data) { this.data = data; this.next = null; }
}

class LinkedList {
    Node head;

    void insertAtPosition(int data, int position) {
        Node newNode = new Node(data);
        if (position == 1) {
            newNode.next = head;
            head = newNode;
            return;
        }
        Node current = head;
        for (int i = 1; i < position - 1 && current != null; i++) {
            current = current.next;
        }
        if (current == null) return;
        newNode.next = current.next;
        current.next = newNode;
    }

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

public class Main {
    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.insertAtPosition(5, 1);
        list.printList();
    }
}
A10 -> 5 -> 20 -> 30 -> null
B10 -> 20 -> 30 -> 5 -> null
C5 -> 10 -> 20 -> 30 -> null
D5 -> 20 -> 10 -> 30 -> null
Attempts:
2 left
💡 Hint
Inserting at position 1 means the new node becomes the head.
🔧 Debug
advanced
2:00remaining
Identify the error in insertAtPosition method
What error will occur when running this code if position is greater than the list length?
DSA Java
void insertAtPosition(int data, int position) {
    Node newNode = new Node(data);
    if (position == 1) {
        newNode.next = head;
        head = newNode;
        return;
    }
    Node current = head;
    for (int i = 1; i < position - 1; i++) {
        current = current.next;
    }
    newNode.next = current.next;
    current.next = newNode;
}
ANullPointerException at current.next when position is too large
BArrayIndexOutOfBoundsException during loop
CNo error, inserts at end correctly
DCompilation error due to missing return
Attempts:
2 left
💡 Hint
Consider what happens if current becomes null before loop ends.
🧠 Conceptual
advanced
1:30remaining
Time complexity of inserting at a specific position in singly linked list
What is the time complexity of inserting a node at position k in a singly linked list of length n?
AO(k) because we traverse to the (k-1)th node
BO(1) because insertion is always at head
CO(n-k) because we traverse from the end backwards
DO(n^2) because of nested loops
Attempts:
2 left
💡 Hint
Think about how many nodes you must visit to reach position k.
🚀 Application
expert
3:00remaining
Result after multiple insertions at various positions
Given an initially empty linked list, what is the final printed list after these insertions? 1) insert 10 at position 1 2) insert 20 at position 2 3) insert 15 at position 2 4) insert 5 at position 1 5) insert 25 at position 5
DSA Java
class Node {
    int data;
    Node next;
    Node(int data) { this.data = data; this.next = null; }
}

class LinkedList {
    Node head;

    void insertAtPosition(int data, int position) {
        Node newNode = new Node(data);
        if (position == 1) {
            newNode.next = head;
            head = newNode;
            return;
        }
        Node current = head;
        for (int i = 1; i < position - 1 && current != null; i++) {
            current = current.next;
        }
        if (current == null) return;
        newNode.next = current.next;
        current.next = newNode;
    }

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

public class Main {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.insertAtPosition(10, 1);
        list.insertAtPosition(20, 2);
        list.insertAtPosition(15, 2);
        list.insertAtPosition(5, 1);
        list.insertAtPosition(25, 5);
        list.printList();
    }
}
A5 -> 10 -> 20 -> 15 -> 25 -> null
B5 -> 10 -> 15 -> 20 -> 25 -> null
C10 -> 5 -> 15 -> 20 -> 25 -> null
D5 -> 15 -> 10 -> 20 -> 25 -> null
Attempts:
2 left
💡 Hint
Insertions shift nodes to the right; track positions carefully.

Practice

(1/5)
1. What does inserting a node at a specific middle position in a linked list mean?
easy
A. Adding a new node exactly at the given position counting from the head
B. Adding a new node only at the start of the list
C. Adding a new node only at the end of the list
D. Replacing the node at the given position with a new node

Solution

  1. Step 1: Understand position-based insertion

    Inserting at a specific position means placing the new node exactly at that index, shifting others if needed.
  2. Step 2: Differentiate from other insertions

    Start or end insertions are special cases, but middle insertion targets a specific index inside the list.
  3. Final Answer:

    Adding a new node exactly at the given position counting from the head -> Option A
  4. Quick Check:

    Insert at position = Insert node at that index [OK]
Hint: Middle insert means place node exactly at given index [OK]
Common Mistakes:
  • Confusing insertion with replacement
  • Thinking insertion only happens at start or end
  • Ignoring zero-based or one-based indexing
2. Which of the following Java code snippets correctly inserts a new node with value 10 at position 3 in a singly linked list?
easy
A. Node newNode = new Node(10); newNode.next = head.next.next.next; head.next.next.next = newNode;
B. Node newNode = new Node(10); newNode.next = head.next; head.next = newNode;
C. Node newNode = new Node(10); newNode.next = head.next.next; head.next.next = newNode;
D. Node newNode = new Node(10); newNode.next = head; head = newNode;

Solution

  1. Step 1: Identify position 3 in zero-based indexing

    Position 3 means after two nodes: head -> next -> next, so newNode.next should point to head.next.next.
  2. Step 2: Link newNode correctly

    Set newNode.next to head.next.next, then update head.next.next to newNode to insert at position 3.
  3. Final Answer:

    Node newNode = new Node(10); newNode.next = head.next.next; head.next.next = newNode; -> Option C
  4. Quick Check:

    Insert at position 3 = link newNode after second node [OK]
Hint: Link new node's next to current node at position, then update previous node [OK]
Common Mistakes:
  • Incorrectly setting newNode.next to wrong node
  • Not updating previous node's next pointer
  • Confusing position indexing
3. What will be the linked list after inserting a node with value 25 at position 2 in the list: 10 -> 20 -> 30 -> 40 -> null?
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);

// Insert 25 at position 2
Node newNode = new Node(25);
Node temp = head;
for (int i = 1; i < 2; i++) {
    temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
medium
A. 25 -> 10 -> 20 -> 30 -> 40 -> null
B. 10 -> 20 -> 30 -> 25 -> 40 -> null
C. 10 -> 20 -> 25 -> 30 -> 40 -> null
D. 10 -> 25 -> 20 -> 30 -> 40 -> null

Solution

  1. Step 1: Traverse to node at position 1

    The loop runs once (i=1), so temp points to node with value 20.
  2. Step 2: Insert newNode after temp

    newNode.next points to temp.next (node 30), then temp.next points to newNode, inserting 25 after 20.
  3. Final Answer:

    10 -> 20 -> 30 -> 25 -> 40 -> null -> Option B
  4. Quick Check:

    Insert at position 2 places 25 after 20 [OK]
Hint: Loop to node before position, link new node next, update previous next [OK]
Common Mistakes:
  • Looping one extra time causing wrong insertion
  • Not updating newNode.next correctly
  • Confusing position indexing
4. Identify the error in the following code that tries to insert a node with value 15 at position 1 in a linked list:
Node newNode = new Node(15);
Node temp = head;
for (int i = 0; i < 1; i++) {
    temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
medium
A. Loop moves temp too far, skipping the head node
B. newNode.next is assigned incorrectly
C. temp.next is not updated after insertion
D. No error, code works fine

Solution

  1. Step 1: Analyze loop movement

    The loop runs once, moving temp from head to head.next, so temp points to node at position 1, not before it.
  2. Step 2: Understand insertion point

    To insert at position 1, temp should point to node before position 1 (head), but here it points to position 1 node, causing incorrect insertion.
  3. Final Answer:

    Loop moves temp too far, skipping the head node -> Option A
  4. Quick Check:

    Loop should stop before position to insert [OK]
Hint: Loop to node before position, not at position [OK]
Common Mistakes:
  • Looping one time too many
  • Confusing position indexing
  • Not handling insertion at position 0 separately
5. Given a singly linked list with nodes: 5 -> 10 -> 15 -> 20 -> null, write the output after inserting a node with value 12 at position 3 using the following code:
Node newNode = new Node(12);
Node temp = head;
int position = 3;
for (int i = 1; i < position - 1; i++) {
    temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;

// Print list
Node current = head;
while (current != null) {
    System.out.print(current.data + " -> ");
    current = current.next;
}
System.out.print("null");
hard
A. 5 -> 10 -> 15 -> 12 -> 20 -> null
B. 12 -> 5 -> 10 -> 15 -> 20 -> null
C. 5 -> 12 -> 10 -> 15 -> 20 -> null
D. 5 -> 10 -> 12 -> 15 -> 20 -> null

Solution

  1. Step 1: Loop to node before position 3

    The loop runs with i=1 only, so temp moves from head (5) to next (10), which is node before position 3.
  2. Step 2: Insert newNode after temp

    newNode.next points to temp.next (15), then temp.next points to newNode, inserting 12 between 10 and 15.
  3. Final Answer:

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

    Insert at position 3 places 12 after 10 [OK]
Hint: Loop to node before position, link new node, update next pointers [OK]
Common Mistakes:
  • Looping too many times causing wrong insertion
  • Not updating newNode.next correctly
  • Confusing position indexing
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