Push Using Linked List Node in DSA Java - Time & Space Complexity
We want to understand how long it takes to add a new node 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.
public class LinkedList {
Node head;
static class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
public void push(int new_data) {
Node new_node = new Node(new_data);
new_node.next = head;
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: Each push runs once per call, no loops or recursion inside.
Adding a node 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 time stays the same no matter how many nodes are in the list.
Time Complexity: O(1)
This means adding a node at the start takes a constant amount of time regardless of list size.
[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 head pointer and the new node's next pointer; no traversal is needed.
Knowing that push on a linked list is fast helps you design efficient data structures and answer questions confidently.
"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 nodes?Solution
Step 1: Understand push operation in linked list
The push operation adds a new element to the start of the linked list by creating a new node and linking it before the current head.Step 2: Compare with other operations
Removing or searching nodes are different operations; push specifically adds at the front.Final Answer:
Adds a new node at the beginning of the list -> Option DQuick Check:
Push = Add at start [OK]
- Confusing push with pop (removal)
- Thinking push adds at the end
- Mixing push with search or delete
Solution
Step 1: Create new node with data
Usenew Node(data)to create a node with the given data.Step 2: Link new node to current head and update head
SetnewNode.next = headand thenhead = newNodeto push at front.Final Answer:
Node newNode = new Node(data); newNode.next = head; head = newNode; -> Option AQuick Check:
Correct syntax for push = Node newNode = new Node(data); newNode.next = head; head = newNode; [OK]
- Forgetting 'new' keyword
- Assigning head before setting newNode.next
- Incorrect order of assignments
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
Node head = null;
// Push 10
Node newNode = new Node(10);
newNode.next = head;
head = newNode;
// Push 20
newNode = new Node(20);
newNode.next = head;
head = newNode;
// Push 30
newNode = new Node(30);
newNode.next = head;
head = newNode;
// Print list
Node temp = head;
while(temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.print("null");Solution
Step 1: Trace push operations
First push 10: list is 10 -> null.
Second push 20: new node 20 points to 10, list is 20 -> 10 -> null.
Third push 30: new node 30 points to 20, list is 30 -> 20 -> 10 -> null.Step 2: Print list
Printing from head prints 30, then 20, then 10, then null.Final Answer:
30 -> 20 -> 10 -> null -> Option AQuick Check:
Push adds at front, last pushed is first [OK]
- Assuming push adds at end
- Confusing order of nodes after pushes
- Printing null at start instead of end
void push(int data) {
Node newNode = new Node(data);
newNode.next = null;
head = newNode;
}Solution
Step 1: Analyze current push method
Currently, newNode.next is set to null, then head is set to newNode. This breaks the link to existing list.Step 2: Correct linking for push
To push correctly, newNode.next should point to current head before head is updated to newNode.Final Answer:
Set newNode.next = head before updating head -> Option BQuick Check:
Link newNode to old head before updating head [OK]
- Setting newNode.next to null breaks list
- Updating head before linking newNode.next
- Not linking new node to existing list
Solution
Step 1: Understand sorted push requirement
To keep list sorted, find the correct position where new node's data fits between nodes.Step 2: Traverse and insert node
Traverse list until next node's data is greater or null, then insert newNode between current and current.next.Final Answer:
Node newNode = new Node(10); Node current = head; while(current.next != null && current.next.data < 10) { current = current.next; } newNode.next = current.next; current.next = newNode; -> Option CQuick Check:
Traverse to insert in sorted order [OK]
- Always pushing at head ignores sorting
- Not updating next pointers correctly
- Inserting at wrong position breaks order
Ready to prove it?
Pass the test and this pattern moves unknown
