0
0
DSA Pythonprogramming~10 mins

Insert at End Tail Insert in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Insert at End Tail Insert
Create new node with data
↓
Is list empty?
↓
Set head = new_node
↓
Traverse until current.next == None
↓
Set current.next = new_node
↓
Done
Create a new node, check if list is empty, if yes set head to new node; else traverse to last node and link new node at end.
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 insert_at_end(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        current = self.head
        while current.next:
            current = current.next
        current.next = new_node

# Insert 10, 20, 30
This code inserts nodes with values 10, 20, 30 at the end of a linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Insert 10Nonehead = new_node(10)BEFORE: None AFTER: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head → node(10)
2Insert 2010current = head(10) current.next = new_node(20)BEFORE: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head → node(10) AFTER: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head → node(10) → node(20)
3Insert 3010 → 20current = head(10) → current = current.next(20) current.next = new_node(30)BEFORE: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head → node(10) → node(20) AFTER: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head → node(10) → node(20) → node(30)
4End10 → 20 → 30No pointer changesFinal list: head → node(10) → node(20) → node(30)
šŸ’” After inserting 30, current.next is None, so insertion ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
headNonenode(10)node(10)node(10)node(10)
currentNoneNonenode(10)node(20)node(20)
new_nodeNonenode(10)node(20)node(30)node(30)
Key Moments - 3 Insights
Why do we check if head is None before traversing?
Because if the list is empty (head is None), we must set head to the new node directly. See execution_table Step 1 where head was None and set to new_node(10).
Why do we stop traversal when current.next is None, not current?
We stop at current.next == None because current is the last node. We want to link new_node to current.next. See execution_table Step 3 where traversal moves until current.next is None before insertion.
What happens to the new node's next pointer after insertion?
The new node's next pointer is always None (null) because it becomes the last node. This is shown in all AFTER visuals where new_node's next is āˆ….
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2, what is the pointer change?
Ahead is set to new_node(20)
Bcurrent.next is set to new_node(20)
Ccurrent is set to new_node(20)
Dhead.next is set to new_node(10)
šŸ’” Hint
Check the 'Pointer Changes' column in Step 2 of execution_table.
At which step does the list first have two nodes?
AStep 2
BStep 1
CStep 3
DStep 4
šŸ’” Hint
Look at the 'Nodes in List' column in execution_table.
If we insert a new node with data 40 after Step 3, what will current be set to during traversal?
Ahead (node 10)
Bnode 20
Cnode 30
DNone
šŸ’” Hint
Traversal moves until current.next is None; after Step 3, node 30 is last.
Concept Snapshot
Insert at End (Tail Insert) in Linked List:
- Create new node with data
- If list empty (head is None), set head = new node
- Else traverse from head until last node (current.next == None)
- Set last node's next = new node
- New node's next = None (tail node)
Full Transcript
To insert at the end of a linked list, first create a new node with the given data. Check if the list is empty by seeing if head is None. If empty, set head to the new node. If not empty, start at head and move through nodes until you find the last node where next is None. Then set that last node's next pointer to the new node. The new node's next pointer is always None because it becomes the last node. This process is repeated for each insertion, growing the list at the tail.