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 singly linked list
Node* pop(Node*& head) {
if (head == nullptr) return nullptr; // empty list
Node* temp = head;
head = head->next;
temp->next = nullptr;
return temp;
}
This code removes the first node from the list and returns it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing and updating the head pointer once.
- How many times: Exactly once per 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 | 4 steps |
| 100 | 4 steps |
| 1000 | 4 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 a constant amount of time regardless of list size.
[X] Wrong: "Removing a node from a linked list always takes time proportional to the list size because you have to find the node first."
[OK] Correct: Here, we remove the first node directly using the head pointer, so no searching is needed. The operation is constant time.
Knowing that popping the first node is quick helps you design efficient linked list operations and shows you understand pointer updates well.
"What if we wanted to pop the last node instead? How would the time complexity change?"
Practice
pop() operation do in a linked list when implemented using nodes?Solution
Step 1: Understand pop operation in linked list context
Pop means removing the top or first element from the list, similar to popping from a stack.Step 2: Identify what happens to the first node
The first node is removed and its value is returned, and the head pointer moves to the next node.Final Answer:
Removes the first node and returns its value -> Option AQuick Check:
Pop = Remove first node [OK]
- Thinking pop adds a node
- Confusing pop with delete all
- Assuming pop searches for a value
Solution
Step 1: Understand how to move head pointer
To remove the first node, head must point to the second node, which is head->next.Step 2: Check each option
head = head->next; correctly assigns head to head->next. head->next = head; reverses pointers incorrectly. delete head->next; deletes the second node, not the first. head = NULL; sets head to NULL, deleting entire list.Final Answer:
head = head->next; -> Option CQuick Check:
Move head to next node = head = head->next; [OK]
- Deleting wrong node
- Setting head to NULL incorrectly
- Reversing pointer assignment
struct Node {
int data;
Node* next;
};
Node* head = new Node{10, nullptr};
head->next = new Node{20, nullptr};
int popValue = head->data;
head = head->next;
std::cout << popValue << " -> " << (head ? std::to_string(head->data) : "null") << std::endl;Solution
Step 1: Trace initial list nodes
head points to node with data 10, which points to node with data 20.Step 2: Execute pop operation
popValue stores 10 (head's data), then head moves to next node (20).Step 3: Print values
Output prints popValue (10) and head's data (20), separated by ' -> '.Final Answer:
10 -> 20 -> Option BQuick Check:
Pop value = 10, new head = 20 [OK]
- Printing new head data as pop value
- Confusing order of output
- Not updating head pointer
int pop(Node*& head) {
int val = head->data;
delete head;
head = head->next;
return val;
}Solution
Step 1: Analyze order of operations
The code deletes head node first, then tries to access head->next, which is invalid after deletion.Step 2: Identify correct order
Must save head->next before deleting head to avoid accessing freed memory.Final Answer:
Accessing head->next after deleting head -> Option AQuick Check:
Delete before accessing next = error [OK]
- Deleting node before saving next
- Ignoring NULL head check
- Wrong return type assumption
Which code snippet correctly implements this?
Solution
Step 1: Understand safe pop and print
We must print the current node's data, save the current node pointer, move head to next, then delete the saved node.Step 2: Check each option
while(head) { int val = head->data; head = head->next; std::cout << val << " "; } does not delete nodes, causing memory leak. while(head) { int val = head->data; Node* temp = head; head = head->next; delete temp; std::cout << val << " "; } correctly saves node in temp, moves head, deletes temp, and prints value. while(head) { delete head; head = head->next; } deletes head before moving, causing invalid access. while(head) { std::cout << head->data << " "; delete head; } deletes head but does not move head pointer, causing infinite loop or crash.Final Answer:
while(head) { int val = head->data; Node* temp = head; head = head->next; delete temp; std::cout << val << " "; } -> Option DQuick Check:
Save node, move head, delete node, print value [OK]
- Deleting node before moving head
- Not deleting nodes causing leaks
- Not moving head pointer
Ready to prove it?
Pass the test and this pattern moves unknown
