Delete Node at Specific Position in DSA C - Time & Space Complexity
We want to understand how the time to delete a node at a certain position in a 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 deleteNode(struct Node** head_ref, int position) {
if (*head_ref == NULL) return;
struct Node* temp = *head_ref;
if (position == 0) {
*head_ref = temp->next;
free(temp);
return;
}
for (int i = 0; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) return;
struct Node* next = temp->next->next;
free(temp->next);
temp->next = next;
}
This code deletes a node at a given position in a singly linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that moves through the list to reach the node before the one to delete.
- How many times: Up to position - 1 times, depending on the position given.
As the list gets longer, deleting a node near the start takes fewer steps, but deleting near the end takes more steps.
| Input Size (n) | Approx. Operations (steps to reach position) |
|---|---|
| 10 | Up to 9 steps |
| 100 | Up to 99 steps |
| 1000 | Up to 999 steps |
Pattern observation: The number of steps grows roughly in direct proportion to the position, which can be as large as the list size.
Time Complexity: O(n)
This means the time to delete a node grows linearly with the size of the list in the worst case.
[X] Wrong: "Deleting a node at any position always takes the same constant time."
[OK] Correct: Because we must first find the node before the one to delete, which can take longer if the position is far in the list.
Understanding how linked list operations scale helps you explain your code clearly and shows you know how data size affects performance.
"What if the list was doubly linked? How would the time complexity of deleting a node at a specific position change?"
Practice
Solution
Step 1: Understand position 1 in linked list
Position 1 refers to the head node of the list.Step 2: Deleting the head node
Removing the head means the next node becomes the new head.Final Answer:
The head node is removed and the next node becomes the new head -> Option CQuick Check:
Deleting position 1 removes head [OK]
- Thinking position 1 deletes the last node
- Assuming list stays same after deletion
- Deleting all nodes instead of one
pos in a singly linked list in C?Solution
Step 1: Understand pointer usage for head modification
To delete a node, the function must modify the head pointer if position 1 is deleted, so it needs a pointer to pointer.Step 2: Check function parameters
Only void deleteNode(struct Node** head_ref, int pos); usesstruct Node** head_refallowing head modification.Final Answer:
void deleteNode(struct Node** head_ref, int pos); -> Option BQuick Check:
Use pointer to pointer for head changes [OK]
- Using single pointer and expecting head to change
- Wrong parameter order
- Missing head pointer in parameters
Solution
Step 1: Identify node at position 3
Position 3 node is 30 in the list.Step 2: Remove node 30 and link previous to next
Link node 20 to node 40, skipping 30.Final Answer:
10 -> 20 -> 40 -> 50 -> NULL -> Option AQuick Check:
Deleted 3rd node (30) correctly [OK]
- Deleting wrong node position
- Not updating links correctly
- Removing head instead of position 3
pos in a linked list:
void deleteNode(struct Node** head_ref, int pos) {
struct Node* temp = *head_ref;
if (pos == 1) {
*head_ref = temp->next;
free(temp);
return;
}
for (int i = 1; temp != NULL && i < pos - 1; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) return;
struct Node* next = temp->next->next;
free(temp->next);
temp->next = next;
}
What is the bug in this code?Solution
Step 1: Check deletion at position 1
If list is empty (*head_ref is NULL), temp is NULL, so temp->next causes error.Step 2: Identify missing empty list check
Code should check if temp is NULL before accessing temp->next when pos == 1.Final Answer:
It does not handle empty list when pos is 1 -> Option AQuick Check:
Empty list check missing at head deletion [OK]
- Ignoring empty list case
- Assuming pos is always valid
- Not freeing the deleted node
Solution
Step 1: Check if position 6 exists in list
List has 5 nodes, so position 6 is invalid.Step 2: Behavior on invalid position
Proper delete function should do nothing if position is invalid, leaving list unchanged.Final Answer:
The list remains unchanged because position 6 is invalid -> Option DQuick Check:
Invalid position means no deletion [OK]
- Deleting last node incorrectly
- Crashing due to no position check
- Deleting head by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
