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 bigger?
Analyze the time complexity of the following code snippet.
public class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public class LinkedList {
public Node head;
public void InsertAtBeginning(int value) {
Node newNode = new Node(value);
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 a new node and updating pointers.
- How many times: This happens once per insertion, no loops or recursion involved.
Adding a node at the start always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 steps (create node, set next, update head) |
| 100 | 3 steps (same as above) |
| 1000 | 3 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 beginning takes a fixed amount of time no matter how big the list is.
[X] Wrong: "Adding 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.
Knowing that head insertion is quick shows you understand linked list basics, a key skill in many coding tasks.
"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 existing nodes after it.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
- Assuming it changes the tail node
Solution
Step 1: Create new node and link
Create a new node and set its next pointer to current head.Step 2: Update head pointer
Assign head to the new node to make it the first node.Final Answer:
Node newNode = new Node(data); newNode.next = head; head = newNode; -> Option DQuick Check:
New node points to old head, head updated [OK]
- Not updating head after creating new node
- Assigning head before setting newNode.next
- Creating multiple new nodes unnecessarily
class Node {
public int data;
public Node next;
public Node(int d) { data = d; next = null; }
}
Node head = new Node(10);
head.next = new Node(20);
Node newNode = new Node(5);
newNode.next = head;
head = newNode;
Node temp = head;
while(temp != null) {
Console.Write(temp.data + " -> ");
temp = temp.next;
}
Console.WriteLine("null");Solution
Step 1: Initial list creation
Head points to 10, which points to 20 (10 -> 20 -> null).Step 2: Insert new node at beginning
New node with data 5 points to old head (10), then head updated to new node (5).Step 3: Traverse and print list
Traversal prints 5 -> 10 -> 20 -> null.Final Answer:
5 -> 10 -> 20 -> null -> Option AQuick Check:
New head 5, followed by 10 and 20 [OK]
- Confusing order of nodes after insertion
- Not updating head pointer
- Printing nodes before insertion
Node newNode = new Node(15); newNode.next = newNode; head = newNode;
Solution
Step 1: Analyze newNode.next assignment
newNode.next points to itself, creating a cycle instead of linking to the list.Step 2: Correct pointer assignment
newNode.next should point to current head to insert at beginning properly.Final Answer:
newNode.next should point to head, not newNode -> Option CQuick Check:
Link new node to old head, not itself [OK]
- Setting next pointer to newNode itself
- Not updating head after insertion
- Confusing order of assignments
Solution
Step 1: Understand insertion order effect
Each insert at beginning places new node before current head.Step 2: Insert 10 first, then 20
Inserting 10 first results in list [10, 30, 40, 50]. Then inserting 20 at beginning results in [20, 10, 30, 40, 50].Step 3: Reverse the order to get desired list
To get [10, 20, 30, 40, 50], insert 20 first, then 10.Final Answer:
Insert 20 at beginning, then insert 10 at beginning -> Option AQuick Check:
Last inserted node becomes new head [OK]
- Inserting in given order causes reversed nodes
- Confusing insert at beginning with insert at end
- Not updating head after each insertion
Ready to prove it?
Pass the test and this pattern moves unknown
