Insert at Middle Specific Position in DSA C - Time & Space Complexity
When we insert a new item at a specific middle position in a list, it takes some steps to find the right spot and then add the item.
We want to know how the time needed grows as the list gets bigger.
Analyze the time complexity of the following code snippet.
void insertAtPosition(Node** head, int data, int position) {
Node* newNode = malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (position == 1) {
newNode->next = *head;
*head = newNode;
return;
}
Node* temp = *head;
for (int i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) return; // position out of bounds
newNode->next = temp->next;
temp->next = newNode;
}
This code inserts a new node at a given position in a singly linked list by first moving to the node before that position.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that moves through the list to reach the node before the insertion point.
- How many times: Up to (position - 2) times, which depends on the input position.
As the list gets longer, the number of steps to reach the insertion point grows roughly in proportion to the position.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 9 steps to reach position |
| 100 | Up to 99 steps to reach position |
| 1000 | Up to 999 steps to reach position |
Pattern observation: The time grows linearly as the position increases within the list.
Time Complexity: O(n)
This means the time to insert grows in a straight line with the size of the list or the position where we insert.
[X] Wrong: "Inserting at the middle is always very fast and takes constant time."
[OK] Correct: Because we must first walk through the list to find the right spot, which takes time proportional to the position.
Understanding how insertion time depends on position helps you explain and reason about linked list operations clearly in interviews.
"What if we had a pointer directly to the middle node? How would the time complexity change?"
Practice
Solution
Step 1: Understand the position concept
Inserting at a specific position means counting nodes from the head and placing the new node exactly there.Step 2: Differentiate from other insertions
Insertion at start or end is special cases; here we insert in the middle without removing nodes.Final Answer:
Adding a new node exactly at the given position counting from the head -> Option BQuick Check:
Insert at position = Insert node at that place [OK]
- Confusing insertion with replacement
- Thinking insertion only happens at start or end
- Ignoring the position count from head
Solution
Step 1: Identify correct traversal
To insert at position 3, we must reach node 2 (one before position 3).Step 2: Link new node correctly
Set newNode->next to current->next, then current->next to newNode to insert properly.Final Answer:
Traverse to node 2, set newNode->next = current->next; current->next = newNode; -> Option AQuick Check:
Insert at pos 3 = link after node 2 [OK]
- Inserting at wrong node position
- Not updating newNode->next before linking
- Setting newNode->next to NULL incorrectly
Solution
Step 1: Identify position 3 in the list
Positions: 1=10, 2=20, 3=30, so new node goes before 30.Step 2: Insert new node at position 3
New node with 99 points to 30, node 20 points to new node.Final Answer:
10 -> 20 -> 99 -> 30 -> 40 -> NULL -> Option DQuick Check:
Insert at pos 3 places 99 before 30 [OK]
- Inserting after instead of at position
- Appending at end instead of middle
- Confusing node values and positions
void insertAtPos(Node** head, int data, int pos) {
Node* newNode = malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
Node* temp = *head;
for(int i = 1; i < pos - 1; i++) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}Solution
Step 1: Analyze loop for position 2
Loop should run from i=1 to i < pos-1 to stop at node before insertion point.Step 2: Check temp usage after loop
Original loop runs one time too many, moving temp to node 2 instead of node 1, causing incorrect insertion or NULL access.Final Answer:
Loop runs one time too many causing temp to be NULL -> Option CQuick Check:
Loop overshoots node before position [OK]
- Loop runs to pos instead of pos-1
- Not checking for NULL before accessing temp->next
- Forgetting to allocate memory for new node
Solution
Step 1: Check list length and position
List has 5 nodes; position 6 means insert after last node (append).Step 2: Insert node at end
Since position is one more than length, node 99 is added after node 5.Final Answer:
Node 99 is appended at the end; list becomes 1 -> 2 -> 3 -> 4 -> 5 -> 99 -> NULL -> Option AQuick Check:
Insert at pos > length means append [OK]
- Rejecting valid append position
- Replacing last node instead of appending
- Inserting at wrong position due to off-by-one
Ready to prove it?
Pass the test and this pattern moves unknown
