Delete Node by Value in DSA Python - Time & Space Complexity
When we delete a node by value from a linked list, we want to know how long it takes as the list grows.
We ask: How does the time needed change when the list gets bigger?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, val):
self.val = val
self.next = None
def delete_node(head, value):
if not head:
return None
if head.val == value:
return head.next
current = head
while current.next and current.next.val != value:
current = current.next
if current.next:
current.next = current.next.next
return head
This code removes the first node with the given value from a singly linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the linked list nodes one by one.
- How many times: Up to n times, where n is the number of nodes in the list.
As the list gets longer, the time to find the node grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps |
| 100 | Up to 100 steps |
| 1000 | Up to 1000 steps |
Pattern observation: The time grows directly with the number of nodes; doubling nodes roughly doubles the steps.
Time Complexity: O(n)
This means the time to delete a node grows linearly with the list size.
[X] Wrong: "Deleting a node by value always takes constant time because we just remove it."
[OK] Correct: We must first find the node, which can take time proportional to the list length.
Understanding this helps you explain how linked lists work and how operations scale, a key skill in many coding discussions.
"What if the list was doubly linked? How would the time complexity change when deleting a node by value?"
Practice
Delete Node by Value do in a linked list?Solution
Step 1: Understand the operation
Deleting a node by value means removing the first node that matches the given value from the linked list.Step 2: Compare options
Only Removes the first node that contains the given value describes removing the first node with the given value. Others describe different operations.Final Answer:
Removes the first node that contains the given value -> Option AQuick Check:
Delete Node by Value = Remove first matching node [OK]
- Confusing deletion by value with adding a node
- Thinking it deletes all nodes with the value
- Mixing deletion by value with reversing the list
Solution
Step 1: Analyze the deletion logic
Deleting a node requires tracking the previous node to update its next pointer. def delete_node(head, value): prev = None current = head while current: if current.val == value: if prev: prev.next = current.next else: head = current.next break prev = current current = current.next return head correctly uses prev and current pointers and updates links properly.Step 2: Check other options
def delete_node(head, value): current = head while current: if current.val == value: current = current.next break current = current.next only moves current pointer without changing links. def delete_node(head, value): for node in head: if node.val == value: head.remove(node) break return head uses invalid iteration on linked list nodes. def delete_node(head, value): head = [x for x in head if x != value] return head treats linked list as a list, which is incorrect.Final Answer:
Option B code correctly deletes node by value in a singly linked list -> Option BQuick Check:
Proper pointer update = def delete_node(head, value): prev = None current = head while current: if current.val == value: if prev: prev.next = current.next else: head = current.next break prev = current current = current.next return head [OK]
- Not updating previous node's next pointer
- Trying to iterate linked list like a Python list
- Not returning updated head after deletion
class Node:
def __init__(self, val):
self.val = val
self.next = None
def delete_node(head, value):
prev = None
current = head
while current:
if current.val == value:
if prev:
prev.next = current.next
else:
head = current.next
break
prev = current
current = current.next
return head
# Create list
head = Node(1)
head.next = Node(3)
head.next.next = Node(5)
head.next.next.next = Node(3)
head.next.next.next.next = Node(7)
head = delete_node(head, 3)
# Print list
current = head
result = []
while current:
result.append(current.val)
current = current.next
print(result)Solution
Step 1: Identify the first node with value 3
The list is 1 -> 3 -> 5 -> 3 -> 7. The first node with value 3 is the second node.Step 2: Delete the first 3 and link previous node to next
After deletion, the list becomes 1 -> 5 -> 3 -> 7.Final Answer:
[1, 5, 3, 7] -> Option AQuick Check:
First 3 removed, rest unchanged = [1, 5, 3, 7] [OK]
- Deleting all nodes with value 3 instead of first only
- Not updating head when first node matches
- Skipping nodes after deletion
def delete_node(head, value):
current = head
while current.next:
if current.next.val == value:
current.next = current.next.next
break
current = current.next
return headSolution
Step 1: Analyze deletion logic
The code checks current.next.val to find the node to delete, so it cannot delete the head node if it matches the value.Step 2: Check edge cases
If the head node has the value, the code never deletes it because it only looks at current.next.Final Answer:
Fails when the node to delete is the head node -> Option DQuick Check:
Head node deletion not handled = Fails when the node to delete is the head node [OK]
- Not handling deletion of head node separately
- Assuming current.next always exists without check
- Ignoring empty list case
Solution
Step 1: Delete node with value 10
Since 10 is the head, deleting it makes 20 the new head. List becomes 20 -> 30 -> 40 -> 50.Step 2: Insert node with value 15 after node with value 20
Find node 20, insert 15 after it. List becomes 20 -> 15 -> 30 -> 40 -> 50.Final Answer:
20 -> 15 -> 30 -> 40 -> 50 -> Option CQuick Check:
Delete head 10, insert 15 after 20 = 20 -> 15 -> 30 -> 40 -> 50 [OK]
- Not updating head after deleting first node
- Inserting new node in wrong position
- Confusing node values and positions
Ready to prove it?
Pass the test and this pattern moves unknown
