Push Using Linked List Node in DSA C++ - 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.
struct Node {
int data;
Node* next;
};
void push(Node*& head, int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = head;
head = newNode;
}
This code adds a new node with a given value at the start of the linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating and linking one new node.
- How many times: Exactly once per push call, no loops or recursion.
Adding a node always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 4 steps (create node, 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 a node at the start takes a constant amount of time no matter how long the list is.
[X] Wrong: "Adding a node at the start takes longer as the list grows because we have to move through the list."
[OK] Correct: We only change the first node's pointer; we never walk through the list, so time does not grow with list size.
Knowing that pushing a node at the start is quick helps you design efficient linked list operations and shows you understand how pointers work.
"What if we changed push to add the node at the end of the 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 inserts a new node at the start (head) of the linked list.Step 2: Effect of push on list structure
The new node becomes the head, and its next pointer points to the previous head node.Final Answer:
Adds a new node at the beginning of the list -> Option DQuick Check:
Push means insert at head = D [OK]
- Confusing push with pop (removal)
- Thinking push adds at the end
- Mixing push with search or delete
Solution
Step 1: Create new node dynamically
Usenew Node(data)to allocate memory and initialize node.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:
Dynamic allocation + pointer update = B [OK]
- Not using pointer (*) for new node
- Using dot (.) instead of arrow (->) for pointers
- Assigning head incorrectly
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
Node* head = nullptr;
// Push 10
Node* newNode1 = new Node(10);
newNode1->next = head;
head = newNode1;
// Push 20
Node* newNode2 = new Node(20);
newNode2->next = head;
head = newNode2;
// Push 30
Node* newNode3 = new Node(30);
newNode3->next = head;
head = newNode3;
// Print list
Node* temp = head;
while(temp != nullptr) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null";Solution
Step 1: Trace push operations
First push 10: list is 10 -> null. Then push 20: list becomes 20 -> 10 -> null. Then push 30: list becomes 30 -> 20 -> 10 -> null.Step 2: Print linked list
Printing from head prints 30, then 20, then 10, then null.Final Answer:
30 -> 20 -> 10 -> null -> Option BQuick Check:
Push adds at head, last pushed is first = C [OK]
- Assuming push adds at tail
- Mixing order of nodes
- Not updating head pointer correctly
void push(Node* head, int data) {
Node* newNode = new Node(data);
newNode->next = head;
head = newNode;
}Solution
Step 1: Analyze parameter passing
head is passed by value, so changes inside function do not affect original head pointer.Step 2: Effect on linked list
Assigning head = newNode only changes local copy; original list head remains unchanged.Final Answer:
head is passed by value, so original list head is not updated -> Option AQuick Check:
Pass pointer by reference or pointer to pointer [OK]
- Ignoring parameter passing method
- Not returning new head if passed by value
- Incorrect pointer usage
Solution
Step 1: Understand stack LIFO behavior
Stack requires last-in, first-out order, so newest element must be accessed first.Step 2: Push at head maintains LIFO
Adding new nodes at head means newest node is always at front, enabling O(1) push and pop.Step 3: Problems with tail insertion
Inserting at tail without tail pointer is inefficient and does not maintain LIFO easily.Final Answer:
Push new nodes at the head of the linked list -> Option CQuick Check:
Stack push = insert at head = A [OK]
- Pushing at tail without tail pointer (slow)
- Not maintaining LIFO order
- Inserting nodes randomly
Ready to prove it?
Pass the test and this pattern moves unknown
