0
0
DSA Pythonprogramming~10 mins

Insert at Beginning Head Insert in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Insert at Beginning Head Insert
Create new node
↓
New node.next = head
↓
head = new node
↓
Done
Insert a new node by making it point to the current head, then update head to this new node.
Execution Sample
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

head = None
# Insert 10 at beginning
new_node = Node(10)
new_node.next = head
head = new_node
This code inserts a new node with value 10 at the beginning of an empty linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with empty listNonehead = NoneList is empty
2Create new node with data=10Node(10)new_node created, next=Noneā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
3Set new_node.next = head (None)Node(10)new_node.next = Noneā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
4Update head = new_nodeNode(10)head → new_nodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
5Insert 20 at beginningNode(20) → Node(10)new_node created, new_node.next = head, head = new_nodeBEFORE: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ next:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head Step 1: new_node.next = head Step 2: head = new_node AFTER: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ ──→ │ data:10│ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6Insert 30 at beginningNode(30) → Node(20) → Node(10)new_node created, new_node.next = head, head = new_nodeBEFORE: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ ──→ │ data:10│ │ data:30│ │ next:──┼──→ │ next:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head Step 1: new_node.next = head Step 2: head = new_node AFTER: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:30│ ──→ │ data:20│ ──→ │ data:10│ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
7End of insertionsNode(30) → Node(20) → Node(10)No changesFinal list: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:30│ ──→ │ data:20│ ──→ │ data:10│ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
šŸ’” No more insertions, list head points to newest node.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5After Step 6Final
headNoneNoneNode(10)Node(20)Node(30)Node(30)
new_nodeNoneNode(10)Node(10)Node(20)Node(30)Node(30)
Key Moments - 3 Insights
Why do we set new_node.next = head before updating head?
Because new_node.next must point to the old first node to keep the list connected. See execution_table step 3 and 4 where new_node.next is set before head changes.
What happens if the list is empty when inserting?
The new node's next points to None (empty list), and head points to the new node. See execution_table steps 1 to 4 for empty list insertion.
Why does head always point to the newest inserted node?
Because we update head = new_node after linking new_node.next to old head. This makes the new node the first in the list. See execution_table steps 4, 5, and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what does head point to?
ANode with data 20
BNode with data 10
CNode with data 30
DNone
šŸ’” Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 5.
At which step does the list first contain more than one node?
AStep 3
BStep 4
CStep 5
DStep 6
šŸ’” Hint
Look at the 'Nodes in List' column to see when multiple nodes appear.
If we skip setting new_node.next = head, what happens to the list?
AList becomes empty
BNew node points to None, old list lost
CHead points to old first node
DNo change in list
šŸ’” Hint
Refer to the pointer changes in steps 3 and 4.
Concept Snapshot
Insert at Beginning (Head Insert):
- Create new node
- Set new_node.next = head
- Update head = new_node
- New node becomes first element
- Works for empty and non-empty lists
Full Transcript
To insert at the beginning of a linked list, first create a new node. Then, link this new node's next pointer to the current head of the list. Finally, update the head pointer to this new node. This makes the new node the first element. If the list was empty, the new node's next points to None. Each insertion updates head to the newest node, preserving the list order. The execution table shows step-by-step how nodes and pointers change, and the variable tracker follows head and new_node references. Key moments clarify why pointer updates happen in this order and what occurs when the list is empty.