0
0
DSA Pythonprogramming

Insert at End Tail Insert in DSA Python

Choose your learning style9 modes available
Mental Model
Add a new item at the very end of a chain by walking to the last item and linking the new one after it.
Analogy: Imagine a line of people holding hands. To add a new person at the end, you walk to the last person and hold their hand.
head -> 1 -> 2 -> 3 -> null
                 ↑
               tail
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null, insert value 4 at end
Goal: Add the value 4 at the end of the list so it becomes 1 -> 2 -> 3 -> 4 -> null
Step 1: start at head node with value 1
head -> [1] -> 2 -> 3 -> null
Why: We begin at the start to find the end of the list
Step 2: move to next node with value 2
head -> 1 -> [2] -> 3 -> null
Why: We keep moving forward to reach the last node
Step 3: move to next node with value 3
head -> 1 -> 2 -> [3] -> null
Why: We continue until we find the node whose next is null
Step 4: create new node with value 4 and link it after node 3
head -> 1 -> 2 -> 3 -> [4] -> null
Why: Linking new node at the end completes the insertion
Result:
head -> 1 -> 2 -> 3 -> 4 -> null
Annotated Code
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

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

    def insert_at_end(self, val):
        new_node = Node(val)  # create new node
        if not self.head:
            self.head = new_node  # empty list, new node is head
            return
        curr = self.head
        while curr.next:
            curr = curr.next  # move to last node
        curr.next = new_node  # link new node at end

    def __str__(self):
        vals = []
        curr = self.head
        while curr:
            vals.append(str(curr.val))
            curr = curr.next
        return ' -> '.join(vals) + ' -> null'

# Driver code
ll = LinkedList()
ll.insert_at_end(1)
ll.insert_at_end(2)
ll.insert_at_end(3)
ll.insert_at_end(4)
print(ll)
new_node = Node(val) # create new node
create a new node to insert
if not self.head: self.head = new_node # empty list, new node is head return
handle empty list by setting head to new node
while curr.next: curr = curr.next # move to last node
advance curr until last node found
curr.next = new_node # link new node at end
link new node after last node
OutputSuccess
1 -> 2 -> 3 -> 4 -> null
Complexity Analysis
Time: O(n) because we must traverse all nodes to reach the end
Space: O(1) because we only create one new node and use constant extra space
vs Alternative: Compared to inserting at the head which is O(1), tail insertion is slower because it requires traversal
Edge Cases
empty list
new node becomes the head directly
DSA Python
if not self.head:
    self.head = new_node  # empty list, new node is head
single element list
new node is linked after the single existing node
DSA Python
while curr.next:
    curr = curr.next  # move to last node
When to Use This Pattern
When you need to add an item at the end of a linked list, use tail insertion by walking to the last node and linking the new node.
Common Mistakes
Mistake: Not checking if the list is empty before traversal
Fix: Add a condition to set head to new node if list is empty before traversing
Mistake: Linking new node before reaching the last node
Fix: Traverse fully until curr.next is null before linking new node
Summary
Adds a new node at the end of a linked list by traversing to the last node and linking it.
Use when you want to keep the existing order and add new data at the tail.
The key is to find the last node whose next is null, then link the new node there.