0
0
DSA Pythonprogramming~10 mins

Delete Node by Value in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Delete Node by Value
Start at head
Check if head is null
YesList empty, stop
No
Check if head value == target
Delete head
Check next node value == target
Delete next node
Stop or continue
Start from the head node, check if it matches the value to delete. If yes, remove it. Otherwise, move through the list to find and delete the node with the target value.
Execution Sample
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def delete_node(head, target):
    if head is None:
        return None
    if head.val == target:
        return head.next
    current = head
    while current.next:
        if current.next.val == target:
            current.next = current.next.next
            break
        current = current.next
    return head
Deletes the first node with the given target value from a singly linked list.
Execution Table
StepCurrent Node ValueCheck ConditionActionList State
1Head (1)Is head None? NoCheck if head.val == 31 -> 2 -> 3 -> 4 -> null
2Head (1)head.val == 3? NoSet current = head1 -> 2 -> 3 -> 4 -> null
3Current (1)current.next exists? YesCheck if current.next.val == 31 -> 2 -> 3 -> 4 -> null
4Current.next (2)2 == 3? NoMove current to current.next1 -> 2 -> 3 -> 4 -> null
5Current (2)current.next exists? YesCheck if current.next.val == 31 -> 2 -> 3 -> 4 -> null
6Current.next (3)3 == 3? YesDelete node with value 3 by linking 2 -> 41 -> 2 -> 4 -> null
7Current (2)Stop traversalReturn head1 -> 2 -> 4 -> null
💡 Node with value 3 found and deleted; traversal stops.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
headNode(1)Node(1)Node(1)Node(1)Node(1)
currentNoneNode(1)Node(2)Node(2)Node(2)
current.nextNode(2)Node(2)Node(3)Node(4)Node(4)
Key Moments - 3 Insights
Why do we check if the head node has the target value before traversing?
Because if the head node itself contains the target value, we must delete it by returning head.next. This is shown in execution_table row 1 and 2 where we check and delete the head if needed.
Why do we check current.next.val instead of current.val during traversal?
We check current.next.val because to delete a node, we need to change the 'next' pointer of the previous node. This is shown in execution_table rows 3 to 6 where current.next is inspected and updated.
What happens if the target value is not found in the list?
The loop finishes without deleting any node, and the original list is returned unchanged. This is implied by the loop condition and no break in the traversal.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what action is performed?
AMove current to next node
BDelete node with value 3 by linking 2 -> 4
CCheck if head is None
DReturn head
💡 Hint
Refer to execution_table row 6 under 'Action' column.
At which step does the traversal stop after deleting the node?
AStep 4
BStep 5
CStep 7
DStep 3
💡 Hint
Look at execution_table row 7 where traversal stops and head is returned.
If the target value was at the head node, what would be the new head after deletion?
AThe node after head
BThe original head node
CNone
DThe last node
💡 Hint
Check execution_table row 1 and 2 where head.val is checked and head.next is returned if matched.
Concept Snapshot
Delete Node by Value in Linked List:
- Check if list is empty (head is None).
- If head has target value, delete by returning head.next.
- Else, traverse list checking current.next for target.
- If found, link current.next to current.next.next to delete.
- Return head after deletion or if not found.
Full Transcript
This visual trace shows how to delete a node by value in a singly linked list. We start by checking if the list is empty. If not, we check if the head node contains the target value. If yes, we delete the head by returning the next node. Otherwise, we traverse the list, checking the next node's value at each step. When we find the target, we delete it by linking the current node to the node after the target. The traversal stops after deletion. If the target is not found, the list remains unchanged. Variables head and current track our position. This step-by-step helps understand pointer changes during deletion.