0
0
DSA Pythonprogramming~10 mins

Delete Node at Beginning in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Delete Node at Beginning
Start
Check if list is empty
Move head to next node
Old head is removed
New head is first node
End
This flow shows checking if the list is empty, then moving the head pointer to the next node to delete the first node.
Execution Sample
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

    def delete_at_beginning(self):
        if self.head is None:
            return
        self.head = self.head.next

    def print_list(self):
        temp = self.head
        while temp:
            print(temp.data, end=' -> ')
            temp = temp.next
        print('None')

ll = LinkedList()
ll.head = Node(10)
ll.head.next = Node(20)
ll.head.next.next = Node(30)

ll.print_list()
ll.delete_at_beginning()
ll.print_list()
This code creates a linked list with nodes 10 -> 20 -> 30, prints it, deletes the first node, then prints the updated list.
Execution Table
StepActionHead Node DataList StateOutput
1Initialize list with nodes 10 -> 20 -> 301010 -> 20 -> 30 -> None10 -> 20 -> 30 -> None
2Call delete_at_beginning()1010 -> 20 -> 30 -> NoneNo output
3Check if head is None1010 -> 20 -> 30 -> NoneNo, head is not None
4Move head to next node2020 -> 30 -> NoneNo output
5Print list after deletion2020 -> 30 -> None20 -> 30 -> None
6End2020 -> 30 -> NoneFinal list printed
💡 Deletion stops after moving head to next node; old head node is removed from list.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
headNode(10)Node(10)Node(20)Node(20)
list state10 -> 20 -> 30 -> None10 -> 20 -> 30 -> None20 -> 30 -> None20 -> 30 -> None
Key Moments - 3 Insights
Why do we check if head is None before deleting?
Because if the list is empty (head is None), there is no node to delete. This prevents errors. See execution_table row 3 where this check happens.
What happens to the old head node after deletion?
The old head node is no longer referenced by the list and is effectively removed. The head pointer moves to the next node. See execution_table row 4.
Does deleting the first node affect the rest of the list?
No, the rest of the list remains intact starting from the new head. Only the first node is removed. See execution_table rows 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the head node data after step 4?
A20
B30
C10
DNone
💡 Hint
Check the 'Head Node Data' column at step 4 in the execution_table.
At which step does the list state change from '10 -> 20 -> 30 -> None' to '20 -> 30 -> None'?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'List State' column in the execution_table to find when the list changes.
If the list was empty initially, which step would cause the deletion to stop?
AStep 4
BStep 3
CStep 5
DStep 2
💡 Hint
Refer to the check for head being None in execution_table row 3.
Concept Snapshot
Delete Node at Beginning in Linked List:
- Check if list is empty (head is None).
- If not empty, move head pointer to next node.
- Old first node is removed automatically.
- List now starts from new head.
- Time complexity: O(1).
Full Transcript
This concept shows how to delete the first node in a linked list. We first check if the list is empty by seeing if the head pointer is None. If it is not empty, we move the head pointer to the next node. This effectively removes the first node from the list. The rest of the list remains unchanged. The code example creates a list with nodes 10, 20, and 30, prints it, deletes the first node, then prints the updated list. The execution table traces each step, showing the head node data and list state before and after deletion. Key moments clarify why the empty check is needed, what happens to the old head, and how the rest of the list is unaffected. The visual quiz tests understanding of these steps. This operation runs in constant time because it only changes the head pointer.