Push Using Linked List Node in DSA Python - Time & Space Complexity
We want to understand how long it takes to add a new item at the start of a linked list.
How does the time needed change as the list grows?
Analyze the time complexity of the following code snippet.
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)
new_node.next = self.head
self.head = new_node
This code adds a new node at the beginning of the linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new node and updating pointers.
- How many times: This happens once per push call, no loops or recursion inside.
Adding a node always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 steps (create node, update pointers) |
| 100 | 3 steps (same as above) |
| 1000 | 3 steps (still the same) |
Pattern observation: The time stays constant regardless of list size.
Time Complexity: O(1)
This means adding a node at the start takes the same short time no matter how many nodes are already in the list.
[X] Wrong: "Adding a node takes longer as the list grows because we have to move through the list."
[OK] Correct: We only change the first node's pointer, so we never walk through the list. The operation is always quick.
Knowing this helps you explain why linked lists are good for quick insertions at the start, a useful skill in many coding problems.
"What if we changed push to add a node at the end of the linked list? How would the time complexity change?"
Practice
push operation do in a linked list when using a node?Solution
Step 1: Understand push operation meaning
Push means adding a new element to the start of the linked list.Step 2: Identify push effect on linked list
The new node becomes the head, pointing to the old head node.Final Answer:
Adds a new node at the beginning of the linked list -> Option DQuick Check:
Push = Add at start [OK]
- Thinking push adds at the end
- Confusing push with pop (removal)
- Assuming push deletes nodes
Solution
Step 1: Create new node with data
Usenew_node = Node(data)to create a node.Step 2: Link new node to current head and update head
Setnew_node.next = headand thenhead = new_node.Final Answer:
new_node = Node(data); new_node.next = head; head = new_node -> Option CQuick Check:
Correct push syntax = new_node = Node(data); new_node.next = head; head = new_node [OK]
- Assigning head before linking new_node.next
- Confusing order of assignments
- Using undefined variables
class Node:
def __init__(self, data):
self.data = data
self.next = None
def push(head, data):
new_node = Node(data)
new_node.next = head
head = new_node
return head
head = None
head = push(head, 10)
head = push(head, 20)
head = push(head, 30)Solution
Step 1: Push 10 to empty list
List becomes 10 -> null.Step 2: Push 20 to front
New head is 20, pointing to 10: 20 -> 10 -> null.Step 3: Push 30 to front
New head is 30, pointing to 20: 30 -> 20 -> 10 -> null.Final Answer:
30 -> 20 -> 10 -> null -> Option AQuick Check:
Push adds at front, last pushed is first [OK]
- Assuming push adds at end
- Not updating head after push
- Mixing order of nodes
def push(head, data):
new_node = Node(data)
head = new_node
new_node.next = head
return headSolution
Step 1: Analyze assignment order
head is set to new_node before linking new_node.next.Step 2: Identify pointer mistake
new_node.next = head sets next to itself, creating a loop.Final Answer:
new_node.next is set after head is updated, causing a loop -> Option BQuick Check:
Link new_node.next before updating head [OK]
- Assigning head before linking next
- Creating self-referential loops
- Forgetting to return head
head = Node(5) head.next = Node(15) head.next.next = Node(25) head = push(head, 35) head = push(head, 45)
Solution
Step 1: Original list is 5 -> 15 -> 25 -> null
Head points to 5, which points to 15, then 25.Step 2: Push 35
New node 35 points to head (5), head updates to 35: 35 -> 5 -> 15 -> 25 -> null.Step 3: Push 45
New node 45 points to head (35), head updates to 45: 45 -> 35 -> 5 -> 15 -> 25 -> null.Final Answer:
45 -> 35 -> 5 -> 15 -> 25 -> null -> Option AQuick Check:
Push adds nodes at front in order [OK]
- Appending instead of pushing at front
- Mixing node order after pushes
- Not updating head after push
Ready to prove it?
Pass the test and this pattern moves unknown
