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 your favorite songs. You want to add a new song exactly at position 3, but you only have a pen and no eraser. You have to rewrite the whole list from scratch to insert the new song in the right place.
Manually rewriting the list every time you want to add a song in the middle is slow and tiring. You might make mistakes, lose track of the order, or accidentally skip songs. It's hard to keep the list neat and correct.
A doubly linked list lets you insert a new item exactly where you want without rewriting everything. You just change a few links before and after the position, and the list updates itself smoothly.
vector<string> songs = {"Song1", "Song2", "Song3", "Song4"};
// To insert at position 3, shift all songs from position 3 onward
songs.resize(5);
for (int i = 4; i > 3; i--) {
songs[i] = songs[i-1];
}
songs[3] = "NewSong";struct Node {
string data;
Node* prev;
Node* next;
};
void insertAtPosition(Node*& head, string newData, int position) {
// Create new node and adjust links to insert at position
}You can quickly and safely add items anywhere in your list without disturbing the rest.
Adding a new contact in the middle of your phone's contact list without scrolling through all contacts or rewriting the entire list.
Manual insertion in the middle is slow and error-prone.
Doubly linked lists let you insert by changing just a few links.
This makes list updates fast, safe, and easy.
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 node.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 BQuick Check:
Insert at position 1 = new head [OK]
- Inserting at position 1 but not updating head pointer
- Not updating previous pointer of old head
- Confusing position 1 with end of list
pos in a doubly linked list in C++?Solution
Step 1: Identify correct pointer updates for insertion
When inserting after temp, newNode's next points to temp's next, and newNode's prev points to temp.Step 2: Update surrounding nodes' pointers
temp's next's prev must point to newNode, and temp's next must point to newNode.Final Answer:
newNode->next = temp->next; temp->next->prev = newNode; temp->next = newNode; newNode->prev = temp; -> Option AQuick Check:
Correct pointer updates = newNode->next = temp->next; temp->next->prev = newNode; temp->next = newNode; newNode->prev = temp; [OK]
- Mixing up next and prev pointers
- Not updating temp->next->prev pointer
- Assigning pointers in wrong order causing broken links
void insertAtPosition(Node*& head, int data, int pos) {
Node* newNode = new Node(data);
if (pos == 1) {
newNode->next = head;
if (head) head->prev = newNode;
head = newNode;
return;
}
Node* temp = head;
for (int i = 1; i < pos - 1 && temp != nullptr; i++) {
temp = temp->next;
}
if (temp == nullptr) return;
newNode->next = temp->next;
if (temp->next) temp->next->prev = newNode;
temp->next = newNode;
newNode->prev = temp;
}Solution
Step 1: Locate position 2 in the list
Position 2 means after the first node (10). So temp points to node with data 10.Step 2: Insert new node with data 15 after temp
New node's next points to 20, and prev points to 10. Update 10's next to new node and 20's prev to new node.Final Answer:
10 <-> 15 <-> 20 <-> 30 <-> null -> Option AQuick Check:
Insert at position 2 places new node after first node [OK]
- Inserting before first node instead of after
- Not updating prev pointer of next node
- Skipping position count in loop
pos in a doubly linked list:void insertAtPosition(Node*& head, int data, int pos) {
Node* newNode = new Node(data);
if (pos == 1) {
newNode->next = head;
if (head) head->prev = newNode;
head = newNode;
return;
}
Node* temp = head;
for (int i = 1; i < pos - 1; i++) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
newNode->prev = temp;
if (newNode->next) newNode->next->prev = newNode;
}Solution
Step 1: Analyze the for loop traversal
The loop moves temp forward but does not check if temp becomes nullptr, risking null pointer dereference.Step 2: Identify missing safety check
Without checking temp != nullptr, accessing temp->next can cause a crash if pos is invalid or list is short.Final Answer:
The loop does not check if temp is nullptr, causing possible crash. -> Option DQuick Check:
Always check temp != nullptr in traversal loops [OK]
- Ignoring nullptr checks in loops
- Not updating head for position 1 insert
- Incorrect order of pointer assignments
Solution
Step 1: Insert 12 at position 3
Position 3 means after second node (10). Insert new node between 10 and 15, updating next and prev pointers accordingly.Step 2: Handle insertion at last position if position > length
Traverse to last node, then insert new node after it, updating last node's next and new node's prev pointers.Final Answer:
5 <-> 10 <-> 12 <-> 15 <-> 20 <-> null; Insert at last by traversing till last node and updating pointers. -> Option CQuick Check:
Insert at position 3 places node between 10 and 15; last insert by traversal [OK]
- Inserting at wrong position
- Not updating prev pointer of next node
- Ignoring last position insertion logic
Ready to prove it?
Pass the test and this pattern moves unknown
