0
0
DSA Pythonprogramming~10 mins

Delete by Value in Doubly Linked List in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Delete by Value in Doubly Linked List
Start at head
Check if current node's value == target?
NoMove to next node
|Yes
Adjust prev node's next pointer
Adjust next node's prev pointer
Delete current node
Done or continue if multiple deletions
End of list reached or node deleted
Start from the head node, check each node's value. If it matches the target, update pointers of previous and next nodes to remove it, then delete the node.
Execution Sample
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.prev = None
        self.next = None

def delete_by_value(head, target):
    current = head
    while current:
        if current.val == target:
            if current.prev:
                current.prev.next = current.next
            if current.next:
                current.next.prev = current.prev
            if current == head:
                head = current.next
            break
        current = current.next
    return head
Deletes the first node with the target value from a doubly linked list and returns the new head.
Execution Table
StepOperationCurrent Node ValuePointer ChangesVisual State
1Start at head1None1 <-> 2 <-> 3 <-> 4 <-> 5
2Check if current.val == 31No change1 <-> 2 <-> 3 <-> 4 <-> 5
3Move to next node2None1 <-> 2 <-> 3 <-> 4 <-> 5
4Check if current.val == 32No change1 <-> 2 <-> 3 <-> 4 <-> 5
5Move to next node3None1 <-> 2 <-> 3 <-> 4 <-> 5
6Check if current.val == 33Yes, delete node1 <-> 2 <-> 4 <-> 5
7Adjust prev.next and next.prev32.next = 4, 4.prev = 21 <-> 2 <-> 4 <-> 5
8Delete current node3Node removed1 <-> 2 <-> 4 <-> 5
9Break loop and return headN/ANone1 <-> 2 <-> 4 <-> 5
💡 Node with value 3 found and deleted; loop exits.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 6Final
current.val1233N/A
headNode(1)Node(1)Node(1)Node(1)Node(1)
list state1 <-> 2 <-> 3 <-> 4 <-> 51 <-> 2 <-> 3 <-> 4 <-> 51 <-> 2 <-> 3 <-> 4 <-> 51 <-> 2 <-> 4 <-> 51 <-> 2 <-> 4 <-> 5
Key Moments - 3 Insights
Why do we need to update both prev.next and next.prev pointers when deleting a node?
Because the list is doubly linked, each node points both ways. Updating prev.next skips the deleted node forward, and updating next.prev skips it backward, fully removing it from the chain (see steps 7 and 8).
What happens if the node to delete is the head of the list?
If the current node is the head, we update the head pointer to current.next to keep the list accessible (step 6 checks and updates head).
Why do we break the loop after deleting the node?
Because this function deletes only the first occurrence of the target value. Continuing would risk errors or deleting multiple nodes unintentionally (step 9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'current.val' at Step 5?
A2
B3
C4
D1
💡 Hint
Check the 'Current Node Value' column at Step 5 in the execution_table.
At which step does the list visually change by removing a node?
AStep 4
BStep 2
CStep 6
DStep 9
💡 Hint
Look at the 'Visual State' column and find when '3' disappears.
If the target value was not found, what would happen to the 'head' variable?
AIt would remain unchanged
BIt would become None
CIt would point to the last node
DIt would point to the deleted node
💡 Hint
Refer to the variable_tracker for 'head' and the exit condition in the execution_table.
Concept Snapshot
Delete by Value in Doubly Linked List:
- Start at head, traverse nodes
- If node.val == target:
  - Update prev.next and next.prev pointers
  - If head node, update head
  - Remove node and stop
- Return updated head
Pointers must be adjusted both ways to unlink node.
Full Transcript
This visualization shows how to delete a node by value in a doubly linked list. We start at the head node and check each node's value. When we find the node with the target value, we update the previous node's next pointer and the next node's previous pointer to skip the current node. If the node to delete is the head, we update the head pointer. Then we remove the node and stop the search. The execution table tracks each step, showing the current node, pointer changes, and the visual state of the list. The variable tracker shows how 'current' and 'head' change during execution. Key moments clarify why both pointers must be updated, what happens if the head is deleted, and why the loop breaks after deletion. The quiz tests understanding of node values at steps, when the list changes, and what happens if the target is not found.