Insert at Specific Position in Doubly Linked List in DSA C++ - Time & Space Complexity
We want to understand how the time needed to insert a node at a specific position in a doubly linked list changes as the list grows.
How does the number of steps grow when the list gets longer?
Analyze the time complexity of the following code snippet.
void insertAtPosition(Node*& head, int pos, int data) {
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; // position out of bounds
newNode->next = temp->next;
if (temp->next) temp->next->prev = newNode;
temp->next = newNode;
newNode->prev = temp;
}
This code inserts a new node at a given position in a doubly linked list by moving through the list to find the right spot.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that moves through the list nodes to reach the position before insertion.
- How many times: Up to pos - 1 times, which depends on the position where we insert.
As the list gets longer, the number of steps to reach the insertion point grows roughly with the position number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps to reach position |
| 100 | Up to 100 steps to reach position |
| 1000 | Up to 1000 steps to reach position |
Pattern observation: The time grows linearly with the position number, so doubling the list size roughly doubles the steps needed.
Time Complexity: O(n)
This means the time to insert depends on how far into the list we want to insert; more nodes mean more steps.
[X] Wrong: "Inserting at any position in a doubly linked list always takes the same constant time."
[OK] Correct: You must first move to the insertion point, which takes time proportional to the position, so it is not always constant time.
Understanding this helps you explain how linked lists work and why some operations are faster or slower, a key skill in many coding discussions.
"What if we kept a pointer to the tail of the list and wanted to insert near the end? How would the time complexity change?"
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
