0
0
DSA Pythonprogramming

Insert at Beginning Head Insert in DSA Python

Choose your learning style9 modes available
Mental Model
Add a new item right at the start so it becomes the first thing in the list.
Analogy: Imagine putting a new book on the front of a line of books on a shelf, pushing the others back.
head -> 1 -> 2 -> 3 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null, insert value 0 at beginning
Goal: Add 0 at the start so the list becomes 0 -> 1 -> 2 -> 3 -> null
Step 1: Create new node with value 0
new_node(0) -> null, head -> 1 -> 2 -> 3 -> null
Why: We need a new node to insert at the beginning
Step 2: Point new node's next to current head (node 1)
new_node(0) -> 1 -> 2 -> 3 -> null, head -> 1 -> 2 -> 3 -> null
Why: Link new node to the existing list so it connects properly
Step 3: Update head to new node (0)
head -> 0 -> 1 -> 2 -> 3 -> null
Why: New node becomes the first node in the list
Result:
head -> 0 -> 1 -> 2 -> 3 -> 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_beginning(self, val):
        new_node = Node(val)  # create new node
        new_node.next = self.head  # link new node to current head
        self.head = new_node  # update head to new node

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

# Driver code
ll = LinkedList()
ll.insert_at_beginning(3)
ll.insert_at_beginning(2)
ll.insert_at_beginning(1)
print("Before insertion:", ll)
ll.insert_at_beginning(0)
print("After insertion:", ll)
new_node = Node(val) # create new node
create a new node with the given value
new_node.next = self.head # link new node to current head
point new node's next to current head to connect list
self.head = new_node # update head to new node
make new node the new head of the list
OutputSuccess
Before insertion: 1 -> 2 -> 3 -> null After insertion: 0 -> 1 -> 2 -> 3 -> null
Complexity Analysis
Time: O(1) because we only change a few pointers without traversing the list
Space: O(1) because we create only one new node regardless of list size
vs Alternative: Compared to inserting at the end which requires O(n) time to find the last node, inserting at the beginning is faster and simpler
Edge Cases
empty list
new node becomes the head and points to null
DSA Python
new_node.next = self.head  # link new node to current head
single element list
new node points to the existing single node, becomes new head
DSA Python
self.head = new_node  # update head to new node
When to Use This Pattern
When you need to add an item quickly at the start of a list, use head insert because it avoids traversing the list.
Common Mistakes
Mistake: Forgetting to update the head pointer to the new node
Fix: Always assign the new node to head after linking it to the old head
Mistake: Not linking the new node's next to the old head, causing list loss
Fix: Set new_node.next to current head before updating head
Summary
Adds a new node at the start of a linked list.
Use when you want to quickly add elements to the front without traversing.
The new node points to the old head, then becomes the new head.