Insert at Beginning Head Insert in DSA Java - 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 {
int data;
Node next;
Node(int data) { this.data = data; this.next = null; }
}
class LinkedList {
Node head;
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 singly linked list.
Look for loops or repeated steps that take time.
- Primary operation: Creating a new node and changing pointers.
- How many times: Exactly once per insertion, no loops 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, no change) |
| 1000 | 3 (still the same fixed steps) |
Pattern observation: The time stays constant regardless of list size.
Time Complexity: O(1)
This means adding at the start takes the same short 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 this helps you explain why linked lists are good for quick insertions at the front, a common question in interviews.
"What if we changed to insert at the end of the list without a tail pointer? 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 changes the tail node
list with value 5?Solution
Step 1: Understand Node constructor
Assuming Node constructor takes value and next node,new Node(5, list.head)creates a new node pointing to current head.Step 2: Assign new node as head
Settinglist.head = new Node(5, list.head);inserts at beginning correctly.Final Answer:
list.head = new Node(5, list.head); -> Option DQuick Check:
New node points to old head, becomes new head [OK]
- Using add() which may add at end
- Changing next of head instead of head itself
- Using insertEnd() which adds at tail
class Node {
int data;
Node next;
Node(int d, Node n) { data = d; next = n; }
}
Node head = new Node(3, null);
head = new Node(2, head);
head = new Node(1, head);
// Print list
Node temp = head;
while(temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.print("null");Solution
Step 1: Trace insertions
First node: 3 -> null
Insert 2 at beginning: 2 -> 3 -> null
Insert 1 at beginning: 1 -> 2 -> 3 -> nullStep 2: Print list
Loop prints nodes in order: 1, 2, 3, then null.Final Answer:
1 -> 2 -> 3 -> null -> Option BQuick Check:
Head insertions reverse order: 1,2,3 [OK]
- Assuming insertion adds at end
- Printing nodes in wrong order
- Confusing next pointers
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
Node head = null;
Node newNode = new Node(10);
newNode.next = head.next;
head = newNode;Solution
Step 1: Analyze head initialization
Head is null initially, sohead.nextaccess is invalid and causes error.Step 2: Identify cause of error
Trying to assignnewNode.next = head.next;throws NullPointerException because head is null.Final Answer:
Accessing head.next when head is null causes NullPointerException -> Option AQuick Check:
Null head means head.next invalid [OK]
- Ignoring null head before accessing next
- Assuming constructor error
- Thinking newNode not assigned to head
Which code snippet correctly performs the insertion and prints the list?
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
Node head = new Node(4);
head.next = new Node(5);
head.next.next = new Node(6);
// Insert new node with value 3 at beginning and print list
// Fill in the blanks below
// Insert code here
// Print code here
Solution
Step 1: Insert new node at beginning
Create new node with value 3, set its next to current head, then update head to new node.Step 2: Print the list
Traverse from head, print each node's data followed by ' -> ', end with 'null'.Final Answer:
Code snippet in Option A correctly inserts and prints the list -> Option AQuick Check:
New node points to old head, head updated [OK]
- Linking new node incorrectly
- Not updating head to new node
- Appending new node at end instead of beginning
Ready to prove it?
Pass the test and this pattern moves unknown
