0
0
DSA Pythonprogramming~10 mins

Delete from Beginning of Doubly Linked List in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Delete from Beginning of Doubly Linked List
Start at head node
↓
Is head null?
Yes→List empty, nothing to delete
No↓
Move head to head.next
↓
Set new head.prev to null
↓
Old head node removed
↓
Done
Start at the head node, check if list is empty. If not, move head to next node and update pointers to remove the first node.
Execution Sample
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

# Delete from beginning
if head is not None:
    head = head.next
    if head is not None:
        head.prev = None
This code deletes the first node of a doubly linked list by moving the head pointer and updating the prev pointer of the new head.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Initial list10 <-> 20 <-> 30 <-> nullhead → node with data 10ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:10│←── │ prev:20│ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
2Check if head is None10 <-> 20 <-> 30 <-> nullNo changeSame as step 1
3Move head to head.next20 <-> 30 <-> nullhead moves from 10 to 20ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:10│←── │ prev:20│←── │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head (old head 10 detached)
4Set new head.prev to None20 <-> 30 <-> nullhead.prev set from 10 to āˆ…ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│←── │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
5Old head node removed20 <-> 30 <-> nullNo pointers to node 10ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│←── │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6End20 <-> 30 <-> nullNo changeFinal list after deletion
šŸ’” Head moved to next node; old head detached and removed from list
Variable Tracker
VariableStartAfter Step 3After Step 4Final
headNode(10)Node(20)Node(20)Node(20)
head.prevNoneNode(10)NoneNone
List size3 nodes3 nodes (old head detached)2 nodes2 nodes
Key Moments - 3 Insights
Why do we set head.prev to None after moving head to head.next?
Because the new head node should not have any node before it. Setting head.prev to None removes the backward link to the old head, as shown in step 4 of the execution_table.
What happens if the list is empty (head is None) when we try to delete?
The code checks if head is None at step 2. If it is None, no deletion happens because there is no node to remove.
Why is the old head node considered removed after moving head?
After step 3, head points to the second node, and no pointers reference the old head node (step 5). This detaches it from the list, effectively deleting it.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does the head pointer point to?
ANode with data 10
BNode with data 30
CNode with data 20
DNone
šŸ’” Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 3 in execution_table.
At which step does the head.prev pointer get set to None?
AStep 4
BStep 3
CStep 2
DStep 5
šŸ’” Hint
Look at the 'Operation' and 'Pointer Changes' columns in execution_table for when head.prev changes.
If the list initially had only one node, what would be the head after deletion?
ANode with data 10
BNone
CNode with data 20
DNode with data 30
šŸ’” Hint
Think about what happens when head.next is None and head moves to head.next.
Concept Snapshot
Delete from Beginning of Doubly Linked List:
- Check if list is empty (head == None)
- Move head to head.next
- Set new head.prev to None
- Old head node is detached and removed
- List size decreases by one
Full Transcript
To delete the first node of a doubly linked list, start at the head. If the list is empty (head is None), do nothing. Otherwise, move the head pointer to the next node. Then set the new head's prev pointer to None to remove backward link to the old head. The old head node is now detached and removed from the list. This reduces the list size by one. The execution steps show the pointer changes and the visual state of the list after each operation.