Delete Node at Specific Position in DSA C# - Time & Space Complexity
We want to know how long it takes to remove a node from a linked list at a certain position.
How does the time needed change when the list gets bigger?
Analyze the time complexity of the following code snippet.
public Node DeleteAtPosition(Node head, int position) {
if (position == 0) {
return head.Next;
}
Node current = head;
for (int i = 0; i < position - 1; i++) {
current = current.Next;
}
current.Next = current.Next.Next;
return head;
}
This code removes the node at the given position from a singly linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop moving through nodes to reach the position before the one to delete.
- How many times: Up to position times, depending on where the node is.
As the list gets longer, the time to reach the node grows with the position number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps to reach the node |
| 100 | Up to 100 steps |
| 1000 | Up to 1000 steps |
Pattern observation: The time grows roughly in direct proportion to the position of the node.
Time Complexity: O(n)
This means the time to delete a node grows linearly with the position in the list.
[X] Wrong: "Deleting a node anywhere always takes the same constant time."
[OK] Correct: You must first reach the node before the one to delete, which takes longer if the node 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 we had a pointer directly to the node before the one to delete? How would the time complexity change?"
Practice
Solution
Step 1: Understand node deletion in linked list
Deleting a node means removing it and connecting the previous node to the next node to keep the list intact.Step 2: Identify the effect of deletion at a specific position
The node at the given position is removed, and the previous node's next pointer is updated to skip the deleted node.Final Answer:
Removing the node at that position and linking its previous node to the next node -> Option BQuick Check:
Delete node = Remove and link previous to next [OK]
- Thinking deletion adds a node
- Confusing deletion with swapping nodes
- Assuming deletion reverses the list
pos in a singly linked list starting with head?Solution
Step 1: Check deletion at head position
If position is 0, deleting means moving head to next node:head = head.next;.Step 2: Validate other options
head.next = null;:head.next = null;deletes second node regardless of pos. head = head.next[pos];:head = head.next[pos];invalid syntax (no array indexing on nodes). head.deleteNode(pos);:head.deleteNode(pos);calls non-existent method.Final Answer:
if (pos == 0) head = head.next; -> Option CQuick Check:
Delete head = head.next [OK]
- Using array-like indexing on linked list
- Calling undefined methods
- Setting next to null incorrectly
1 -> 3 -> 5 -> 7 -> null?Solution
Step 1: Identify node at position 2
Positions start at 0: 0->1, 1->3, 2->5, 3->7. Node at position 2 is 5.Step 2: Delete node 5 and link previous to next
Remove 5, link node with value 3 to node with value 7.Final Answer:
1 -> 3 -> 7 -> null -> Option AQuick Check:
Delete node 5 at pos 2 = 1->3->7 [OK]
- Deleting wrong node by counting from 1
- Not linking previous node to next
- Removing head instead of position 2
pos, what is the error?
Node current = head;
for (int i = 0; i < pos - 1; i++) {
current = current.next;
}
current.next = current.next.next;Solution
Step 1: Analyze loop for position 0
For pos=0, loop conditioni < pos - 1(i < -1) is false, current remainshead, thencurrent.next = current.next.next;deletes SECOND node (pos 1) instead of head.Step 2: Check deletion logic for other positions
For pos >=1, loop moves current to node before target and skips target correctly.Final Answer:
It does not handle deletion at position 0 -> Option AQuick Check:
Code misses head deletion case [OK]
- Ignoring head deletion case
- Assuming loop runs for pos=0
- Confusing syntax errors with logic errors
10 -> 20 -> 30 -> 40 -> 50 -> null. You want to delete the node at position 4. After deletion, what will be the list? Also, what happens if you try to delete at position 5?Solution
Step 1: Delete node at position 4
Positions: 0->10,1->20,2->30,3->40,4->50. Deleting node 50 removes last node, list becomes 10->20->30->40->null.Step 2: Attempt deletion at position 5
Position 5 does not exist. Trying to access node at pos 5 causes null reference error (runtime error).Final Answer:
10 -> 20 -> 30 -> 40 -> null; Deleting at position 5 causes runtime error (null reference) -> Option DQuick Check:
Delete last node works; out-of-range causes error [OK]
- Assuming out-of-range deletion does nothing
- Deleting wrong node at position 5
- Confusing last node position with list length
Ready to prove it?
Pass the test and this pattern moves unknown
