0
0
DSA Pythonprogramming

Push Using Linked List Node in DSA Python

Choose your learning style9 modes available
Mental Model
Adding a new item at the start of a linked list means making it the new head and linking it to the old head.
Analogy: Imagine a line of people holding hands. To add a new person at the front, they hold the new person's hand, and the new person holds the old first person's hand.
head -> 1 -> 2 -> 3 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null, push value 0
Goal: Add the value 0 at the start of the list so it becomes the new head
Step 1: Create new node with value 0
new_node(0) -> null
head -> 1 -> 2 -> 3 -> null
Why: We need a new node to add to the list
Step 2: Set 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 points to the old first node
Step 3: Update head to new node
head -> 0 -> 1 -> 2 -> 3 -> null
Why: Make new node the first node in the list
Result:
head -> 0 -> 1 -> 2 -> 3 -> null
Annotated Code
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

    def push(self, new_data):
        new_node = Node(new_data)  # create new node with data
        new_node.next = self.head  # link new node to current head
        self.head = new_node       # update head to new node

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

# Driver code
llist = LinkedList()
llist.push(3)
llist.push(2)
llist.push(1)
llist.push(0)  # push 0 at front
llist.print_list()
new_node = Node(new_data) # create new node with data
create a new node to add at the front
new_node.next = self.head # link new node to current head
point new node's next to current head to keep list connected
self.head = new_node # update head to new node
make new node the new head of the list
OutputSuccess
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 adding at the end which takes O(n) to find the tail, pushing at front 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 single existing node and 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 element quickly at the start of a list, use the push pattern to insert at head in constant time.
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, breaking the list
Fix: Set new_node.next = head before updating head
Summary
Adds a new node at the start of a linked list by updating pointers.
Use when you want to insert elements quickly at the front of the list.
The new node points to the old head, then becomes the new head.