0
0
DSA Pythonprogramming

Delete Node by Value in DSA Python

Choose your learning style9 modes available
Mental Model
To remove a node with a specific value, find it by moving through the list, then change the previous node to skip it.
Analogy: Imagine a chain of paper clips linked together. To remove one clip, you unhook the clip before it and link it directly to the clip after the one you want to remove.
head -> 1 -> 2 -> 3 -> 4 -> null
          ↑
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> 4, delete value 3
Goal: Remove the node containing value 3 from the list
Step 1: start at head node with value 1
head -> [curr->1] -> 2 -> 3 -> 4 -> null
Why: We begin searching from the start of the list
Step 2: move curr to node with value 2, keep track of previous node (1)
head -> 1 -> [curr->2] -> 3 -> 4 -> null
Why: Check next node to find the target value
Step 3: move curr to node with value 3, previous is 2
head -> 1 -> 2 -> [curr->3] -> 4 -> null
Why: Found the node to delete
Step 4: change previous node's next pointer to skip curr and point to curr's next (4)
head -> 1 -> 2 -> 4 -> null
Why: By skipping the node with value 3, it is removed from the list
Result:
head -> 1 -> 2 -> 4 -> null
Annotated Code
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, val):
        new_node = Node(val)
        if not self.head:
            self.head = new_node
            return
        curr = self.head
        while curr.next:
            curr = curr.next
        curr.next = new_node

    def delete_by_value(self, val):
        curr = self.head
        prev = None
        while curr:
            if curr.val == val:
                if prev:
                    prev.next = curr.next  # skip current node
                else:
                    self.head = curr.next  # delete head node
                return
            prev = curr
            curr = curr.next

    def __str__(self):
        vals = []
        curr = self.head
        while curr:
            vals.append(str(curr.val))
            curr = curr.next
        return ' -> '.join(vals) + ' -> null'

# Driver code
ll = LinkedList()
for v in [1, 2, 3, 4]:
    ll.append(v)
print("Before deletion:", ll)
ll.delete_by_value(3)
print("After deletion:", ll)
while curr:
traverse nodes until end or target found
if curr.val == val:
check if current node holds the value to delete
if prev: prev.next = curr.next # skip current node else: self.head = curr.next # delete head node
remove current node by linking previous node to next, or update head if deleting first node
prev = curr curr = curr.next
advance pointers to continue search
OutputSuccess
Before deletion: 1 -> 2 -> 3 -> 4 -> null After deletion: 1 -> 2 -> 4 -> null
Complexity Analysis
Time: O(n) because in worst case we traverse all n nodes once to find the value
Space: O(1) because we only use a few pointers regardless of list size
vs Alternative: Compared to rebuilding the list without the value, this method removes the node in one pass with no extra space
Edge Cases
empty list
nothing happens, list remains empty
DSA Python
while curr:
value not found
list remains unchanged after full traversal
DSA Python
while curr:
value is at head node
head pointer updates to next node, removing first node
DSA Python
else:
    self.head = curr.next  # delete head node
When to Use This Pattern
When you need to remove a specific value from a linked list, use the delete-by-value pattern to find and unlink the node efficiently.
Common Mistakes
Mistake: Not updating the head when the node to delete is the first node
Fix: Add a check to update the head pointer when prev is None
Mistake: Skipping updating the previous node's next pointer, leaving the node still linked
Fix: Set prev.next to curr.next to unlink the target node
Summary
Deletes the first node with a given value from a linked list.
Use when you want to remove a specific value from anywhere in the list.
Remember to handle the special case when the node to delete is the head.