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.
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void push(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
This code adds a new node at the beginning of a 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 regardless of list size.
| 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 no matter how big the list is.
Time Complexity: O(1)
This means adding a node at the start takes a fixed amount of time, no matter how long the list is.
[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 link the new node; we never traverse the list for push at the front.
Knowing that push at the front is quick helps you choose the right data structure for fast insertions.
"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 the push operation
The push operation adds a new element to the linked list.Step 2: Identify where the new node is added
In a linked list, push adds the new node at the start (head) of the list.Final Answer:
Adds a new node at the beginning of the list -> Option BQuick Check:
Push = Add at head [OK]
- Thinking push adds at the end
- Confusing push with pop
- Assuming push deletes nodes
Solution
Step 1: Allocate memory correctly for new node
Use malloc(sizeof(Node)) to allocate memory for the new node.Step 2: Link new node to current head and update head
Set newNode->next to current head, then update head to newNode.Final Answer:
newNode = malloc(sizeof(Node)); newNode->next = head; head = newNode; -> Option CQuick Check:
Correct malloc and linking = newNode = malloc(sizeof(Node)); newNode->next = head; head = newNode; [OK]
- Forgetting sizeof in malloc
- Assigning head before setting newNode->next
- Using prev pointer in singly linked list
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* head = NULL;
void push(int val) {
Node* newNode = malloc(sizeof(Node));
newNode->data = val;
newNode->next = head;
head = newNode;
}
push(10);
push(20);
push(30);Solution
Step 1: Trace each push call
push(10) adds 10 at head: 10 -> null; push(20) adds 20 at head: 20 -> 10 -> null; push(30) adds 30 at head: 30 -> 20 -> 10 -> null.Step 2: Confirm final linked list order
The last pushed value is at the head, so list is 30 -> 20 -> 10 -> null.Final Answer:
30 -> 20 -> 10 -> null -> Option AQuick Check:
Last pushed is head = 30 -> 20 -> 10 [OK]
- Assuming push adds at tail
- Reading list in wrong order
- Confusing push with append
void push(int val) {
Node* newNode;
newNode->data = val;
newNode->next = head;
head = newNode;
}Solution
Step 1: Check newNode initialization
newNode is declared but not allocated memory with malloc, so it points to garbage.Step 2: Fix by allocating memory
Use newNode = malloc(sizeof(Node)) before accessing newNode->data or newNode->next.Final Answer:
newNode is not allocated memory; fix by malloc(sizeof(Node)) -> Option DQuick Check:
Always malloc before using newNode [OK]
- Using uninitialized pointer
- Assigning data before allocation
- Changing head before linking newNode
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* head = NULL;
void push(int val) {
Node* newNode = malloc(sizeof(Node));
newNode->data = val;
newNode->next = head;
head = newNode;
}
// Your code here to push and printSolution
Step 1: Push values in correct order
Push 5, then 15, then 25. Each push adds at head, so list is 25 -> 15 -> 5 -> null.Step 2: Print all nodes correctly
Use a loop while(temp) to print each node's data followed by '->', then print 'null' after loop.Final Answer:
push(5); push(15); push(25); Node* temp = head; while(temp) { printf("%d -> ", temp->data); temp = temp->next; } printf("null\n"); -> Option AQuick Check:
Push order + full print loop = push(5); push(15); push(25); Node* temp = head; while(temp) { printf("%d -> ", temp->data); temp = temp->next; } printf("null\n"); [OK]
- Pushing in reverse order
- Using while(temp->next) and missing last node
- Not printing 'null' at end
Ready to prove it?
Pass the test and this pattern moves unknown
