Insert at Beginning Head Insert 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 bigger?
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 insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
This code adds a new node at the start of a linked list by adjusting pointers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating a new node and updating pointers.
- How many times: This happens once per insert, no loops or traversals involved.
Adding at the start always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 (create node, set next, update head) |
| 100 | 3 (same steps) |
| 1000 | 3 (still the same steps) |
Pattern observation: The number of steps stays the same regardless of list size.
Time Complexity: O(1)
This means adding at the beginning takes a fixed amount of time no matter how big the list is.
[X] Wrong: "Inserting at the start takes longer as the list grows because we have to move all nodes."
[OK] Correct: We only change a few pointers; we do not move or visit other nodes, so time stays constant.
Knowing that head insertion is quick helps you choose the right method when speed matters in linked lists.
"What if we changed to insert at the end of the list? How would the time complexity change?"
Practice
insert at beginning do in a linked list?Solution
Step 1: Understand the meaning of 'insert at beginning'
Inserting at the beginning means adding a new element before the current first element.Step 2: Identify the effect on the linked list
The new node becomes the new head, pushing the old head to second position.Final Answer:
Adds a new node at the start of the list -> Option BQuick Check:
Insert at beginning = Add new head [OK]
- Confusing insert at beginning with insert at end
- Thinking it deletes nodes instead of adding
- Assuming it changes the last node
Solution
Step 1: Understand pointer assignment for head insert
To insert at beginning, new_node's next points to current head.Step 2: Update head to new_node
Then head is updated to new_node to make it the first node.Final Answer:
new_node.next = head; head = new_node -> Option DQuick Check:
Set new_node.next to head, then head = new_node [OK]
- Assigning head.next instead of new_node.next
- Not updating head to new_node
- Swapping assignment order incorrectly
class Node:
def __init__(self, data):
self.data = data
self.next = None
head = Node(10)
new_node = Node(20)
new_node.next = head
head = new_node
Solution
Step 1: Create initial list with head node 10
Initially, head points to node with data 10 and next is None.Step 2: Insert new_node with data 20 at beginning
new_node.next points to head (node 10), then head updated to new_node.Final Answer:
20 -> 10 -> null -> Option AQuick Check:
New node 20 becomes head, points to 10 [OK]
- Thinking new_node is added at end
- Not updating head to new_node
- Confusing node data order
class Node:
def __init__(self, data):
self.data = data
self.next = None
head = Node(5)
new_node = Node(15)
head = new_node
new_node.next = head
Solution
Step 1: Analyze assignment order
head is set to new_node first, then new_node.next is set to head (which is now new_node itself).Step 2: Identify logical error
This causes new_node.next to point to itself, losing original list starting at old head.Final Answer:
The order of assignments is wrong; new_node.next should be set before head update -> Option AQuick Check:
Set new_node.next before head = new_node [OK]
- Updating head before linking new_node.next
- Confusing attribute initialization
- Assuming head must be None initially
40 -> 30 -> 20 -> 10 -> null?Solution
Step 1: Create new node with value 40
new_node is created with data 40 and next initially None.Step 2: Link new_node to current head
Set new_node.next to current head (node 30) to keep existing list after new node.Step 3: Update head to new_node
Head pointer is updated to new_node, making it the first node.Final Answer:
new_node = Node(40); new_node.next = head; head = new_node -> Option CQuick Check:
Link new node to old head, then update head [OK]
- Linking head.next instead of new_node.next
- Updating head before linking new_node.next
- Setting new_node.next to None losing list
Ready to prove it?
Pass the test and this pattern moves unknown
