Pop Using Linked List Node in DSA Javascript - Time & Space Complexity
We want to understand how long it takes to remove the last item from a linked list.
How does the time needed change as the list grows bigger?
Analyze the time complexity of the following code snippet.
function pop(head) {
if (!head) return null;
if (!head.next) return null; // only one node
let current = head;
while (current.next.next) {
current = current.next;
}
current.next = null;
return head;
}
This code removes the last node from a singly linked list by walking to the second last node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through nodes one by one.
- How many times: It runs once for each node until the second last node.
As the list gets longer, the loop runs more times, roughly one less than the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 9 |
| 100 | 99 |
| 1000 | 999 |
Pattern observation: The operations grow almost directly with the number of nodes.
Time Complexity: O(n)
This means the time to pop the last node grows linearly with the list size.
[X] Wrong: "Popping the last node is always quick and constant time."
[OK] Correct: Because singly linked lists don't have backward links, you must walk through nodes to find the one before last, so time grows with list size.
Knowing how linked list operations scale helps you explain your choices clearly and shows you understand data structure behavior.
"What if we kept a pointer to the last node? How would the time complexity of pop change?"
Practice
pop() operation do in a linked list implemented using nodes?Solution
Step 1: Understand pop operation in linked list
The pop operation removes the last node from the linked list and returns its value.Step 2: Differentiate from other operations
Adding a node is push, removing first node is shift, and peeking returns value without removal.Final Answer:
Removes the last node and returns its value -> Option DQuick Check:
pop() removes last node = B [OK]
- Confusing pop with push (adding nodes)
- Thinking pop removes the first node
- Assuming pop only returns value without removal
Solution
Step 1: Identify standard method name
In JavaScript linked list implementations,pop()is the standard method to remove the last node.Step 2: Check other options
Other method names likepopNode(),removeLast(), ordeleteTail()are not standard or common.Final Answer:
list.pop() -> Option CQuick Check:
pop() removes last node syntax = D [OK]
- Using non-standard method names
- Confusing pop() with other removal methods
- Assuming popNode() is built-in
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
push(value) {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
pop() {
if (!this.head) return null;
if (!this.head.next) {
const val = this.head.value;
this.head = null;
return val;
}
let current = this.head;
while (current.next.next) {
current = current.next;
}
const val = current.next.value;
current.next = null;
return val;
}
}
const list = new LinkedList();
list.push(10);
list.push(20);
list.push(30);
const poppedValue = list.pop();
console.log(poppedValue);What will be printed?
Solution
Step 1: Trace push operations
Nodes added in order: 10 -> 20 -> 30Step 2: Trace pop operation
Pop removes last node (30) and returns its value.Final Answer:
30 -> Option AQuick Check:
Last node value popped = 30 [OK]
- Confusing pop with removing first node
- Expecting 20 instead of 30
- Assuming pop returns null if list not empty
pop() method for a singly linked list has a bug. What is the error?pop() {
if (!this.head) return null;
let current = this.head;
while (current.next) {
current = current.next;
}
const val = current.value;
current = null;
return val;
}Solution
Step 1: Analyze traversal to last node
The code moves current to last node correctly but does not track previous node.Step 2: Check removal logic
Setting current = null only changes local variable, does not remove last node from list.Step 3: Identify missing update
Previous node's next should be set to null to remove last node.Final Answer:
It does not update the previous node's next to null -> Option AQuick Check:
Missing previous.next = null causes bug = A [OK]
- Not tracking previous node during traversal
- Setting current = null does not remove node
- Ignoring empty list check
You perform two pop operations in a row. What is the state of the linked list after these operations?
Solution
Step 1: Initial list nodes
List is 5 -> 15 -> 25 -> 35 -> null.Step 2: First pop removes last node
Pop removes 35, list becomes 5 -> 15 -> 25 -> null.Step 3: Second pop removes new last node
Pop removes 25, list becomes 5 -> 15 -> null.Final Answer:
5 -> 15 -> null -> Option BQuick Check:
Two pops remove last two nodes = A [OK]
- Removing nodes from the front instead of end
- Removing only one node after two pops
- Assuming list becomes empty after two pops
Ready to prove it?
Pass the test and this pattern moves unknown
