Bird
Raised Fist0
DSA C#programming~20 mins

Insert at Specific Position in Doubly Linked List in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Insertion 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 doubly linked list
What is the printed state of the doubly linked list after inserting a node with value 25 at position 3?
DSA C#
class Node {
    public int data;
    public Node prev, next;
    public Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
    public Node head;

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

    public string PrintList() {
        Node temp = head;
        string result = "";
        while (temp != null) {
            result += temp.data + " <-> ";
            temp = temp.next;
        }
        result += "null";
        return result;
    }
}

DoublyLinkedList dll = new DoublyLinkedList();
dll.InsertAtPosition(10, 1);
dll.InsertAtPosition(20, 2);
dll.InsertAtPosition(30, 3);
dll.InsertAtPosition(25, 3);
string output = dll.PrintList();
Console.WriteLine(output);
A10 <-> 20 <-> 25 <-> 30 <-> null
B10 <-> 25 <-> 20 <-> 30 <-> null
C10 <-> 20 <-> 30 <-> 25 <-> null
D25 <-> 10 <-> 20 <-> 30 <-> null
Attempts:
2 left
💡 Hint
Remember that position 3 means the new node goes after the second node.
❓ Predict Output
intermediate
2:00remaining
Output after inserting at head in doubly linked list
What is the printed state of the doubly linked list after inserting a node with value 5 at position 1?
DSA C#
class Node {
    public int data;
    public Node prev, next;
    public Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
    public Node head;

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

    public string PrintList() {
        Node temp = head;
        string result = "";
        while (temp != null) {
            result += temp.data + " <-> ";
            temp = temp.next;
        }
        result += "null";
        return result;
    }
}

DoublyLinkedList dll = new DoublyLinkedList();
dll.InsertAtPosition(10, 1);
dll.InsertAtPosition(20, 2);
dll.InsertAtPosition(30, 3);
dll.InsertAtPosition(5, 1);
string output = dll.PrintList();
Console.WriteLine(output);
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 new head.
🔧 Debug
advanced
2:00remaining
Identify the error in insertion at position code
What error will occur when running this code to insert at position 4 in a list of 3 nodes?
DSA C#
class Node {
    public int data;
    public Node prev, next;
    public Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
    public Node head;

    public void InsertAtPosition(int data, int position) {
        Node newNode = new Node(data);
        Node temp = head;
        for (int i = 1; i < position - 1; i++) {
            temp = temp.next;
        }
        newNode.next = temp.next;
        if (temp.next != null) temp.next.prev = newNode;
        temp.next = newNode;
        newNode.prev = temp;
    }
}

DoublyLinkedList dll = new DoublyLinkedList();
dll.InsertAtPosition(10, 1);
dll.InsertAtPosition(20, 2);
dll.InsertAtPosition(30, 3);
dll.InsertAtPosition(40, 4);
ANullReferenceException at temp.next when position is beyond list length
BIndexOutOfRangeException due to loop bounds
CNo error, insertion works correctly at end
DStackOverflowException due to infinite recursion
Attempts:
2 left
💡 Hint
Check what happens if temp becomes null before accessing temp.next.
❓ Predict Output
advanced
2:00remaining
Output after inserting at tail in doubly linked list
What is the printed state of the doubly linked list after inserting a node with value 40 at position 5 in a list of 4 nodes?
DSA C#
class Node {
    public int data;
    public Node prev, next;
    public Node(int d) { data = d; prev = next = null; }
}

class DoublyLinkedList {
    public Node head;

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

    public string PrintList() {
        Node temp = head;
        string result = "";
        while (temp != null) {
            result += temp.data + " <-> ";
            temp = temp.next;
        }
        result += "null";
        return result;
    }
}

DoublyLinkedList dll = new DoublyLinkedList();
dll.InsertAtPosition(10, 1);
dll.InsertAtPosition(20, 2);
dll.InsertAtPosition(30, 3);
dll.InsertAtPosition(35, 4);
dll.InsertAtPosition(40, 5);
string output = dll.PrintList();
Console.WriteLine(output);
A10 <-> 20 <-> 30 <-> 35 <-> null
B10 <-> 20 <-> 30 <-> 35 <-> 40 <-> null
C10 <-> 20 <-> 30 <-> 40 <-> 35 <-> null
D40 <-> 10 <-> 20 <-> 30 <-> 35 <-> null
Attempts:
2 left
💡 Hint
Inserting at position 5 in a 4-node list adds the node at the end.
🧠 Conceptual
expert
2:00remaining
Effect of incorrect prev pointer update in doubly linked list insertion
What is the consequence of forgetting to update the prev pointer of the node after the inserted node in a doubly linked list insertion?
AThe prev pointer is not important, so no effect occurs
BThe inserted node will be skipped during forward traversal
CThe list will automatically fix the pointers during garbage collection
DThe list may become corrupted, causing traversal errors or infinite loops
Attempts:
2 left
💡 Hint
Think about how doubly linked lists rely on both next and prev pointers for correct navigation.

Practice

(1/5)
1. What happens when you insert a new node at position 1 in a doubly linked list?
easy
A. The new node is ignored because position 1 is invalid.
B. The new node is added at the end of the list.
C. The new node becomes the new head of the list.
D. The list is reversed before insertion.

Solution

  1. Step 1: Understand position 1 in doubly linked list

    Position 1 means the start of the list, so the new node should be the first element.
  2. Step 2: Insert new node at head

    The new node's next pointer points to the old head, and the old head's previous pointer points back to the new node.
  3. Final Answer:

    The new node becomes the new head of the list. -> Option C
  4. Quick Check:

    Insert at position 1 = new head [OK]
Hint: Position 1 means new head insertion [OK]
Common Mistakes:
  • Thinking position 1 means end of list
  • Ignoring previous pointer update
  • Assuming position 1 is invalid
2. Which of the following is the correct syntax to insert a new node at position 3 in a doubly linked list in C#?
easy
A. InsertAtPosition(newNode, 3);
B. InsertAtPosition(3, newNode);
C. InsertNodeAt(3, newNode);
D. AddNodeAtPosition(newNode, 3);

Solution

  1. Step 1: Check method parameter order

    The common pattern is to pass the node first, then the position.
  2. Step 2: Match method name and parameters

    InsertAtPosition(newNode, 3) matches the expected syntax for inserting at position 3.
  3. Final Answer:

    InsertAtPosition(newNode, 3); -> Option A
  4. Quick Check:

    Node first, position second = InsertAtPosition(newNode, 3); [OK]
Hint: Node comes before position in method call [OK]
Common Mistakes:
  • Swapping parameter order
  • Using incorrect method names
  • Passing position before node
3. Given the doubly linked list: 10 <-> 20 <-> 30 <-> null, what is the list after inserting 15 at position 2?
medium
A. 10 <-> 15 <-> 20 <-> 30 <-> null
B. 15 <-> 10 <-> 20 <-> 30 <-> null
C. 10 <-> 20 <-> 15 <-> 30 <-> null
D. 10 <-> 20 <-> 30 <-> 15 <-> null

Solution

  1. Step 1: Identify position 2 in the list

    Position 2 means after the first node (10), so new node goes between 10 and 20.
  2. Step 2: Insert 15 between 10 and 20

    Update pointers: 10's next to 15, 15's previous to 10, 15's next to 20, 20's previous to 15.
  3. Final Answer:

    10 <-> 15 <-> 20 <-> 30 <-> null -> Option A
  4. Quick Check:

    Insert at position 2 = between first and second nodes [OK]
Hint: Position 2 means insert after first node [OK]
Common Mistakes:
  • Inserting before first node
  • Inserting at wrong position
  • Not updating previous pointers
4. What is the error in the following code snippet for inserting at position 3 in a doubly linked list?
Node newNode = new Node(25);
Node temp = head;
for (int i = 1; i < 2; i++) {
    temp = temp.next;
}
newNode.next = temp.next;
newNode.prev = temp;
temp.next.prev = newNode;
temp.next = newNode;
medium
A. The loop should run until i < 2, not i < 3.
B. temp.next.prev = newNode; causes null reference if temp.next is null.
C. newNode.prev should be set to temp.prev instead of temp.
D. The newNode.next assignment is missing.

Solution

  1. Step 1: Analyze loop and temp position

    The loop moves temp to the node at position 2, so temp.next might be null if list ends there.
  2. Step 2: Check pointer updates for null safety

    Accessing temp.next.prev without checking if temp.next is null can cause a runtime error.
  3. Final Answer:

    temp.next.prev = newNode; causes null reference if temp.next is null. -> Option B
  4. Quick Check:

    Check for null before accessing temp.next.prev [OK]
Hint: Always check temp.next for null before accessing prev [OK]
Common Mistakes:
  • Ignoring null pointer checks
  • Wrong loop range causing off-by-one
  • Incorrect pointer assignments
5. You have a doubly linked list: 5 <-> 10 <-> 15 <-> 20 <-> null. You want to insert a node with value 12 at position 3. After insertion, what will be the correct order of nodes?
medium
A. 5 <-> 10 <-> 15 <-> 20 <-> 12 <-> null
B. 5 <-> 12 <-> 10 <-> 15 <-> 20 <-> null
C. 12 <-> 5 <-> 10 <-> 15 <-> 20 <-> null
D. 5 <-> 10 <-> 12 <-> 15 <-> 20 <-> null

Solution

  1. Step 1: Identify position 3 in the list

    Position 3 means the new node should be placed after the second node (10) and before the third node (15).
  2. Step 2: Insert node with value 12 between 10 and 15

    Update pointers: 10's next to 12, 12's previous to 10, 12's next to 15, 15's previous to 12.
  3. Final Answer:

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

    Insert at position 3 places node between second and third nodes [OK]
Hint: Insert at position n means between nodes n-1 and n [OK]
Common Mistakes:
  • Inserting at wrong position
  • Not updating both next and prev pointers
  • Confusing position numbering
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