Delete Node at Specific Position in DSA Go - 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.
How does the number of steps grow when the list gets longer?
Analyze the time complexity of the following code snippet.
func deleteAtPosition(head *Node, pos int) *Node {
if pos == 0 {
return head.next
}
current := head
for i := 0; i < pos-1; i++ {
current = current.next
}
current.next = current.next.next
return head
}
This code deletes the node at the given position in a singly linked list by moving to the node before it and changing links.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that moves through the list nodes.
- How many times: It runs (pos - 1) times to reach the node before the one to delete.
As the position to delete moves further in the list, the steps increase roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 9 steps |
| 100 | Up to 99 steps |
| 1000 | Up to 999 steps |
Pattern observation: The number of steps grows directly with the position number, so longer lists or deleting nodes near the end take more time.
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 at any position always takes the same time."
[OK] Correct: Because you must first move to the node before the one to delete, which takes more steps if the position is farther in the list.
Understanding how deletion time grows helps you explain your code clearly and shows you know how linked lists work under the hood.
"What if we had a doubly linked list? How would the time complexity change when deleting a node at a specific position?"
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 what happens at the 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 neighbors [OK]
- Thinking deletion adds a node
- Swapping nodes instead of deleting
- Only counting nodes without deletion
pos in a singly linked list starting with head?Solution
Step 1: Check deletion at position 0
If position is 0, we update head to head.next to remove the first node.Step 2: Traverse to node before position and update links
For other positions, we move to node before the target and set its next to skip the deleted node.Final Answer:
if pos == 0 { head = head.next } else { curr := head; for i := 0; i < pos-1; i++ { curr = curr.next }; curr.next = curr.next.next } -> Option CQuick Check:
Correct traversal and link update = if pos == 0 { head = head.next } else { curr := head; for i := 0; i < pos-1; i++ { curr = curr.next }; curr.next = curr.next.next } [OK]
- Not handling position 0 separately
- Looping one extra time causing nil pointer
- Incorrectly updating head inside loop
type Node struct {
data int
next *Node
}
// Assume head points to 10
// Delete node at position 2
func deleteAtPosition(head *Node, pos int) *Node {
if pos == 0 {
return head.next
}
curr := head
for i := 0; i < pos-1; i++ {
curr = curr.next
}
curr.next = curr.next.next
return head
}Solution
Step 1: Identify node at position 2
Position 2 node is the third node with value 30.Step 2: Update links to remove node 30
Node at position 1 (20) next pointer is updated to node at position 3 (40), skipping 30.Final Answer:
10 -> 20 -> 40 -> 50 -> null -> Option AQuick Check:
Deleted 30 at pos 2 = 10->20->40->50 [OK]
- Deleting wrong node index
- Not updating links correctly
- Returning wrong head pointer
pos but causes a runtime error. What is the error and how to fix it?
func deleteAtPosition(head *Node, pos int) *Node {
curr := head
for i := 0; i < pos; i++ {
curr = curr.next
}
curr.next = curr.next.next
return head
}Solution
Step 1: Analyze loop traversal
The loop moves curr to the node at position pos, but we need the node before pos to update links.Step 2: Fix loop to stop at node before pos
Change loop condition to i < pos-1 so curr points to previous node, allowing safe link update.Final Answer:
Logic error because loop goes to pos instead of pos-1; fix loop to i < pos-1 -> Option AQuick Check:
Loop to pos-1 for previous node [OK]
- Looping to pos causing nil pointer
- Not handling pos 0 case
- Assuming curr.next always exists
Solution
Step 1: Delete node at position 0
Remove first node (5), list becomes 10 -> 15 -> 20 -> 25 -> null.Step 2: Delete node at position 2
Positions now: 0=10,1=15,2=20,3=25. Remove node 20, list becomes 10 -> 15 -> 25 -> null.Step 3: Delete node at position 1
Positions now: 0=10,1=15,2=25. Remove node 15, list becomes 10 -> 25 -> null.Final Answer:
10 -> 25 -> null -> Option DQuick Check:
Delete 0,2,1 in order = 10->25 [OK]
- Not updating positions after deletions
- Deleting wrong nodes due to stale positions
- Confusing zero-based indexing
Ready to prove it?
Pass the test and this pattern moves unknown
