Insert at Beginning Head Insert in DSA C - 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.
struct Node {
int data;
struct Node* next;
};
void insertAtBeginning(struct Node** head, int newData) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = newData;
newNode->next = *head;
*head = newNode;
}
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 and linking one new node.
- How many times: Exactly once per insertion, no loops or recursion.
Adding a node at the start always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 4 steps (allocate, assign data, link, update head) |
| 100 | 4 steps (same as above) |
| 1000 | 4 steps (same as above) |
Pattern observation: The number of steps stays the same regardless of list size.
Time Complexity: O(1)
This means adding at the start takes a fixed amount of time no matter how big the list is.
[X] Wrong: "Adding at the beginning takes longer as the list grows because we have to move all nodes."
[OK] Correct: We only change the head pointer and the new node's next pointer; no nodes are moved or visited.
Knowing that inserting at the start is quick helps you choose the right method for fast updates in linked lists.
"What if we changed to insert at the end of the list? How would the time complexity change?"
Practice
Solution
Step 1: Understand the operation
Inserting at the beginning means placing a new node before the current first node.Step 2: Effect on the list
The new node becomes the head, pushing the old head to second position.Final Answer:
Adds a new node at the start of the list -> Option CQuick Check:
Insert at beginning = Add new head [OK]
- Confusing insert at beginning with insert at end
- Thinking it deletes nodes
- Assuming it inserts in the middle
Solution
Step 1: Assign newNode's next to current head
This links the new node to the existing list.Step 2: Update head to newNode
Now the new node is the first node in the list.Final Answer:
newNode->next = head; head = newNode; -> Option AQuick Check:
Link newNode to head, then update head [OK]
- Swapping assignments order
- Assigning head to newNode->next incorrectly
- Not updating head pointer
Solution
Step 1: Insert 10 at beginning
List becomes 10 -> null.Step 2: Insert 20 at beginning
List becomes 20 -> 10 -> null.Step 3: Insert 30 at beginning
List becomes 30 -> 20 -> 10 -> null.Final Answer:
30 -> 20 -> 10 -> null -> Option DQuick Check:
Last inserted is new head [OK]
- Assuming insertion at end
- Reversing insertion order
- Confusing node values order
struct Node {
int data;
struct Node* next;
};
void insertAtBeginning(struct Node** head, int val) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
*head = newNode;
}Solution
Step 1: Check newNode->next assignment
It is set to NULL, losing the existing list.Step 2: Correct linking
newNode->next should point to current head before head is updated.Final Answer:
newNode->next should point to current head before updating head -> Option BQuick Check:
Link newNode->next to old head before head update [OK]
- Setting newNode->next to NULL always
- Not using double pointer for head
- Forgetting to assign data
Solution
Step 1: Current list is 5 -> 15 -> 25 -> null
This is the starting list before insertion.Step 2: Insert 10 at beginning
New node 10 points to current head (5), then head updated to 10.Final Answer:
10 -> 5 -> 15 -> 25 -> null -> Option AQuick Check:
New node becomes head, old head follows [OK]
- Inserting at wrong position
- Not updating head pointer
- Linking nodes incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
