Delete Node at Specific Position in DSA C++ - Time & Space Complexity
When deleting a node at a specific position in a linked list, we want to know how the time needed changes as the list grows.
We ask: How much work does the program do when the list gets bigger?
Analyze the time complexity of the following code snippet.
void deleteNode(Node*& head, int position) {
if (position == 0) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* current = head;
for (int i = 0; i < position - 1; i++) {
current = current->next;
}
Node* temp = current->next;
current->next = temp->next;
delete temp;
}
This code deletes the node at the 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: It runs (position - 1) times, which depends on the position given.
As the list gets longer, deleting a node near the start takes less time, but deleting one near the end takes more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 9 steps to reach the node |
| 100 | Up to 99 steps to reach the node |
| 1000 | Up to 999 steps to reach the node |
Pattern observation: The number of steps grows roughly in direct proportion to the position of the node, 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 list size in the worst case.
[X] Wrong: "Deleting a node at any position always takes the same constant time."
[OK] Correct: Because to delete a node, you must first reach it by moving through the list, which takes more time if the node is far from the start.
Understanding how linked list operations scale helps you explain your code clearly and shows you know how data size affects performance.
"What if we had a doubly linked list with a tail pointer? How would the time complexity for deleting a node at the end change?"
Practice
Solution
Step 1: Understand position 1 in linked list
Position 1 refers to the head node of the list.Step 2: Delete 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 AQuick Check:
Deleting position 1 removes the 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: Check deletion at position 1
If position is 1, we update head to head->next to remove the first node.Step 2: Verify syntax correctness
if (pos == 1) { head = head->next; } correctly updates head pointer; others are invalid or incorrect for singly linked list.Final Answer:
if (pos == 1) { head = head->next; } -> Option BQuick Check:
Update head for position 1 deletion [OK]
- Using prev pointer in singly linked list
- Setting next pointer to NULL incorrectly
- Deleting wrong node pointer
1 -> 2 -> 3 -> 4 -> 5 -> NULL?Solution
Step 1: Identify node at position 3
Node at position 3 is the node with value 3.Step 2: Remove node 3 and link previous to next
Link node 2's next to node 4, skipping node 3.Final Answer:
1 -> 2 -> 4 -> 5 -> NULL -> Option DQuick Check:
Node 3 removed, list links 2 to 4 [OK]
- Removing wrong node
- Not updating previous node's next pointer
- Deleting head instead of position 3
void deleteNode(Node*& head, int pos) {
Node* temp = head;
for (int i = 1; i < pos - 1; i++) {
temp = temp->next;
}
Node* toDelete = temp->next;
temp->next = toDelete->next;
delete toDelete;
}Solution
Step 1: Analyze loop and pointer usage
The loop moves temp to node before position to delete, but no checks if temp or temp->next is NULL.Step 2: Identify missing validation
Without checking if position is valid or temp is NULL, code may crash on invalid positions.Final Answer:
No check if position is valid or if temp is NULL -> Option AQuick Check:
Missing boundary checks cause runtime errors [OK]
- Ignoring invalid position inputs
- Assuming list length is sufficient
- Not checking for NULL pointers
10 -> 20 -> 30 -> 40 -> 50 -> NULL. Write the resulting list after deleting the node at position 5 and then deleting the node at position 1.Solution
Step 1: Delete node at position 5
Node at position 5 is 50, removing it results in list: 10 -> 20 -> 30 -> 40 -> NULL.Step 2: Delete node at position 1
Deleting head node 10 updates head to 20, final list: 20 -> 30 -> 40 -> NULL.Final Answer:
20 -> 30 -> 40 -> NULL -> Option CQuick Check:
Delete last then head node sequentially [OK]
- Deleting head first then last causing wrong list
- Not updating head after deleting position 1
- Forgetting to remove last node
Ready to prove it?
Pass the test and this pattern moves unknown
