Delete Node at Specific Position in DSA Python - 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 as the list gets bigger?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, data):
self.data = data
self.next = None
def delete_node(head, position):
if position == 0:
return head.next
current = head
for _ in range(position - 1):
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 the list to reach the node before the one to delete.
- How many times: Up to position - 1 times, depending on where the node is.
As the list grows, the time to reach the node grows too, because we move step by step.
| 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 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 of the node in the list.
[X] Wrong: "Deleting a node always takes the same short time no matter where it is."
[OK] Correct: Because we must first walk through the list to find the node before the one to delete, so deleting near the end takes longer.
Understanding how linked list operations scale helps you explain your code clearly and shows you know how data structures work under the hood.
"What if we had a doubly linked list? How would the time complexity of deleting a node at a specific position change?"
Practice
Solution
Step 1: Understand position 0 in linked list
Position 0 refers to the head node of the list.Step 2: Effect of deleting head node
Deleting 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 0 removes head [OK]
- Thinking last node is deleted at position 0
- Assuming list stays same after deletion
- Deleting all nodes instead of one
pos in a singly linked list in Python?Solution
Step 1: Check deletion at position 0
Deleting at position 0 means moving head to next node.Step 2: Verify syntax correctness
if pos == 0: head = head.next correctly assigns head to head.next when pos is 0.Final Answer:
if pos == 0: head = head.next -> Option BQuick Check:
Correct syntax for deleting head node [OK]
- Using head.prev which doesn't exist in singly linked list
- Wrong condition for position check
- Assigning head incorrectly
class Node:
def __init__(self, data):
self.data = data
self.next = None
def delete_node(head, pos):
if pos == 0:
return head.next
current = head
for _ in range(pos - 1):
current = current.next
current.next = current.next.next
return head
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
head.next.next.next = Node(40)
head = delete_node(head, 2)
# Print list
current = head
result = []
while current:
result.append(current.data)
current = current.next
print(result)Solution
Step 1: Identify node at position 2
Position 2 node has value 30 in the list 10 -> 20 -> 30 -> 40.Step 2: Delete node at position 2
Node 20's next pointer is updated to node 40, skipping node 30.Final Answer:
[10, 20, 40] -> Option AQuick Check:
Deleted node 30 at position 2 [OK]
- Deleting wrong node due to off-by-one error
- Not updating next pointer correctly
- Returning wrong head
pos in a singly linked list:
def delete_node(head, pos):
if pos == 0:
head = head.next
current = head
for _ in range(pos - 1):
current = current.next
current.next = current.next.next
return headSolution
Step 1: Check deletion at position 0
When pos is 0, head is updated but not returned immediately.Step 2: Effect of missing return
Code continues and tries to access current.next causing errors or wrong behavior.Final Answer:
Missing return after deleting head node -> Option DQuick Check:
Return immediately after deleting head [OK]
- Not returning head after deleting first node
- Ignoring edge cases when list is empty
- Assuming current.next always exists
Solution
Step 1: Delete node at position 0
Deleting head (5), list becomes 10 -> 15 -> 20 -> 25.Step 2: Delete node at position 2
Node at position 2 is 20, deleting it results in 10 -> 15 -> 25.Step 3: Delete node at position 1
Node at position 1 is 15, deleting it results in 10 -> 25.Final Answer:
[10, 25] -> Option AQuick Check:
Sequential deletions update list correctly [OK]
- Not updating list after each deletion
- Confusing positions after deletions
- Deleting wrong nodes due to static indexing
Ready to prove it?
Pass the test and this pattern moves unknown
