Delete Node at Specific Position in DSA Java - Time & Space Complexity
When we delete a node at a specific position in a linked list, we want to know how the time it takes changes as the list grows.
We ask: How much work does the computer do when the list gets bigger?
Analyze the time complexity of the following code snippet.
public void deleteAtPosition(int position) {
if (head == null) return;
if (position == 0) {
head = head.next;
return;
}
Node current = head;
for (int i = 0; i < position - 1; i++) {
if (current == null || current.next == null) return;
current = current.next;
}
if (current == null || current.next == null) return;
current.next = current.next.next;
}
This code removes the node at the given position from 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 up to position - 1 times, depending on the position given.
As the position to delete moves further in the list, the loop runs more times, so the work grows with position.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 9 steps to reach position 9 |
| 100 | Up to 99 steps to reach position 99 |
| 1000 | Up to 999 steps to reach position 999 |
Pattern observation: The number of steps grows roughly in a straight line with the position number.
Time Complexity: O(n)
This means the time to delete a node grows linearly with the position of the node in the list.
[X] Wrong: "Deleting a node at any position always takes the same short time."
[OK] Correct: Because to delete a node, you must first reach it by moving through the list, which takes longer for nodes farther down.
Understanding how time grows with list size helps you explain your code clearly and shows you know what affects performance in real tasks.
"What if we had a doubly linked list? How would the time complexity of deleting 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: 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 CQuick Check:
Deleting position 1 removes head [OK]
- Thinking position 1 means last node
- Assuming list stays same after deletion
- Deleting all nodes instead of one
pos in a singly linked list starting with head?Solution
Step 1: Check deletion at position 1
If position is 1, we remove the head by pointing head to head.next.Step 2: Validate syntax correctness
if (pos == 1) head = head.next; correctly assigns head = head.next; others either skip nodes or nullify incorrectly.Final Answer:
if (pos == 1) head = head.next; -> Option AQuick Check:
Correct syntax for deleting head node [OK]
- Skipping two nodes instead of one
- Setting head.next to null incorrectly
- Setting entire head to null without condition
Solution
Step 1: Identify node at position 3
Position 3 node is 30 in the list 10 -> 20 -> 30 -> 40 -> null.Step 2: Remove node 30 and link previous to next
Link node 20's next to node 40, skipping 30.Final Answer:
10 -> 20 -> 40 -> null -> Option AQuick Check:
Deleted 3rd node 30 correctly [OK]
- Deleting wrong node position
- Not updating previous node's next pointer
- Removing head instead of position 3
pos in a singly linked list:Node temp = head;
for (int i = 1; i < pos - 1; i++) {
temp = temp.next;
}
temp.next = temp.next.next;What error can occur if
pos is 1?Solution
Step 1: Analyze loop for pos = 1
Loop runs from i=1 to pos-2 = -1, so it does not run; temp = head.Step 2: Access temp.next when pos=1
temp.next is head.next, but deletion logic expects previous node; deleting head this way causes NullPointerException.Final Answer:
NullPointerException because temp.next is null -> Option BQuick Check:
Deleting position 1 needs special handling [OK]
- Assuming loop runs for pos=1
- Not handling head deletion separately
- Expecting no error for pos=1
Solution
Step 1: Delete node at position 5
Position 5 node is 25; after deletion list: 5 -> 10 -> 15 -> 20 -> null.Step 2: Delete node at position 1
Delete head (5); new list: 10 -> 15 -> 20 -> null.Step 3: Delete node at position 2
Position 2 node is 15; after deletion list: 10 -> 20 -> null.Final Answer:
10 -> 20 -> null -> Option DQuick Check:
Sequential deletions update list correctly [OK]
- Deleting wrong nodes after list changes
- Not updating head after first deletion
- Confusing positions after deletions
Ready to prove it?
Pass the test and this pattern moves unknown
