Pop Using Linked List Node in DSA C - Time & Space Complexity
We want to understand how long it takes to remove the first item from a linked list.
How does the time needed change when the list grows bigger?
Analyze the time complexity of the following code snippet.
// Remove the first node from a linked list
Node* pop(Node** head) {
if (*head == NULL) return NULL;
Node* temp = *head;
*head = (*head)->next;
temp->next = NULL;
return temp;
}
This code removes the first node from the linked list and returns it.
- Primary operation: Accessing and updating the head pointer once.
- How many times: Exactly once per pop call, no loops or recursion.
Removing the first node always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 steps |
| 100 | 5 steps |
| 1000 | 5 steps |
Pattern observation: The number of steps stays the same as the list grows.
Time Complexity: O(1)
This means removing the first node takes the same short time no matter how big the list is.
[X] Wrong: "Removing a node takes longer if the list is bigger because you have to look through all nodes."
[OK] Correct: We only remove the first node by changing the head pointer, no need to check other nodes.
Knowing this helps you explain why linked lists are good for quick removals at the front, a common question in interviews.
"What if we wanted to remove the last node instead? How would the time complexity change?"
Practice
pop operation do in a linked list implemented as a stack?Solution
Step 1: Understand pop operation in stack
Pop removes the top element from the stack, which is the first node in a linked list stack.Step 2: Apply to linked list
In a linked list used as a stack, the first node is removed and returned during pop.Final Answer:
Removes and returns the first node (top) of the linked list -> Option CQuick Check:
Pop = remove first node [OK]
- Thinking pop removes last node
- Confusing pop with push
- Assuming pop deletes entire list
Solution
Step 1: Check parameter passing for head pointer
To modify the head pointer, we pass a pointer to pointer (node** head).Step 2: Verify pop logic
Store current head in temp, move head to next node, return temp node.Final Answer:
node* pop(node** head) { node* temp = *head; *head = (*head)->next; return temp; } -> Option AQuick Check:
Pointer to pointer needed to update head [OK]
- Passing head by value instead of pointer to pointer
- Returning wrong node or void
- Not updating head pointer correctly
typedef struct node { int data; struct node* next; } node;
node* pop(node** head) {
if (*head == NULL) return NULL;
node* temp = *head;
*head = (*head)->next;
return temp;
}
int main() {
node n3 = {3, NULL};
node n2 = {2, &n3};
node n1 = {1, &n2};
node* head = &n1;
node* popped = pop(&head);
printf("%d -> %d -> %d -> NULL", head->data, head->next->data, head->next->next->data);
return 0;
}Solution
Step 1: Trace pop operation
Pop removes node with data 1, head now points to node with data 2.Step 2: Trace printf arguments
head->data = 2, head->next->data = 3, head->next->next = NULL, accessing ->data causes segmentation fault after printing "2 -> 3 -> " .Final Answer:
2 -> 3 -> (segmentation fault) -> Option DQuick Check:
Accessing NULL->data causes crash [OK]
- Assuming NULL prints as 'NULL' string
- Ignoring segmentation fault on NULL dereference
- Misreading linked list after pop
node* pop(node* head) {
node* temp = head;
head = head->next;
return temp;
}Solution
Step 1: Analyze head pointer update
Function updates local copy of head, original head outside remains unchanged.Step 2: Fix pointer update
Pass pointer to pointer (node**) to modify original head pointer.Final Answer:
Pass head as node** and update *head inside function -> Option AQuick Check:
Use node** to update head pointer [OK]
- Updating local copy of head only
- Not passing pointer to pointer
- Returning wrong node
Solution
Step 1: Understand popping all nodes
Pop removes top node each time, so loop until list is empty.Step 2: Print and free nodes in loop
Print data of popped node immediately, then free memory to avoid leaks.Final Answer:
Use a loop: while head != NULL, pop node, print data, free node -> Option BQuick Check:
Loop pop-print-free until empty [OK]
- Recursing without printing in correct order
- Not freeing nodes causing memory leaks
- Stopping after one pop
Ready to prove it?
Pass the test and this pattern moves unknown
