What if you could add anything anywhere in your list instantly without rewriting everything?
Why Insert at Specific Position in Doubly Linked List in DSA C#?
Imagine you have a paper list of names and you want to add a new name exactly in the middle. You have to rewrite the whole list to fit the new name in the right spot.
Manually rewriting the list every time you add a name is slow and easy to mess up. You might lose names or put the new one in the wrong place.
Using a doubly linked list lets you jump to the exact spot and insert the new name without rewriting the whole list. It keeps the order and links both forward and backward smoothly.
string[] names = {"Alice", "Bob", "Charlie"};
// To insert "David" at position 2
var newNames = new string[names.Length + 1];
for (int i = 0; i < 2; i++) newNames[i] = names[i];
newNames[2] = "David";
for (int i = 2; i < names.Length; i++) newNames[i + 1] = names[i];DoublyLinkedList list = new DoublyLinkedList(); list.InsertAtPosition(2, "David");
You can add items anywhere in your list quickly and safely without breaking the order or losing data.
In a music playlist app, you want to add a new song exactly after the third song without rearranging the whole playlist.
Manual insertion is slow and error-prone.
Doubly linked lists let you insert anywhere efficiently.
This keeps your data organized and easy to update.
Practice
Solution
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.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.Final Answer:
The new node becomes the new head of the list. -> Option CQuick Check:
Insert at position 1 = new head [OK]
- Thinking position 1 means end of list
- Ignoring previous pointer update
- Assuming position 1 is invalid
Solution
Step 1: Check method parameter order
The common pattern is to pass the node first, then the position.Step 2: Match method name and parameters
InsertAtPosition(newNode, 3) matches the expected syntax for inserting at position 3.Final Answer:
InsertAtPosition(newNode, 3); -> Option AQuick Check:
Node first, position second = InsertAtPosition(newNode, 3); [OK]
- Swapping parameter order
- Using incorrect method names
- Passing position before node
Solution
Step 1: Identify position 2 in the list
Position 2 means after the first node (10), so new node goes between 10 and 20.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.Final Answer:
10 <-> 15 <-> 20 <-> 30 <-> null -> Option AQuick Check:
Insert at position 2 = between first and second nodes [OK]
- Inserting before first node
- Inserting at wrong position
- Not updating previous pointers
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;Solution
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.Step 2: Check pointer updates for null safety
Accessing temp.next.prev without checking if temp.next is null can cause a runtime error.Final Answer:
temp.next.prev = newNode; causes null reference if temp.next is null. -> Option BQuick Check:
Check for null before accessing temp.next.prev [OK]
- Ignoring null pointer checks
- Wrong loop range causing off-by-one
- Incorrect pointer assignments
Solution
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).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.Final Answer:
5 <-> 10 <-> 12 <-> 15 <-> 20 <-> null -> Option DQuick Check:
Insert at position 3 places node between second and third nodes [OK]
- Inserting at wrong position
- Not updating both next and prev pointers
- Confusing position numbering
Ready to prove it?
Pass the test and this pattern moves unknown
