Pop Using Linked List Node in DSA Typescript - 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?
Analyze the time complexity of the following code snippet.
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
function pop(head: Node | null): Node | null {
if (!head) return null;
if (!head.next) return null;
let current = head;
while (current.next && current.next.next) {
current = current.next;
}
current.next = null;
return head;
}
This code removes the last node from a singly linked list by walking through nodes until the second last one.
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, so about n-2 times for n nodes.
As the list gets longer, the loop runs more times, roughly one less than the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 8 steps |
| 100 | About 98 steps |
| 1000 | About 998 steps |
Pattern observation: The number of steps grows roughly in a straight line with the number of nodes.
Time Complexity: O(n)
This means the time to remove the last node grows directly with the list size.
[X] Wrong: "Removing the last node is always quick and takes constant time."
[OK] Correct: Because we must walk through the list to find the second last node, the time depends on list length.
Knowing how linked list operations scale helps you explain trade-offs clearly and shows you understand data structure behavior.
"What if the linked list kept a pointer to the last node? How would the time complexity of pop 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 last element from the list, similar to popping from a stack.Step 2: Identify what pop returns
Pop removes the last node and returns its stored value.Final Answer:
Removes the last node and returns its value -> Option AQuick Check:
Pop = remove last node [OK]
- Confusing pop with push operation
- Thinking pop removes the first node
- Assuming pop searches for a value
Solution
Step 1: Check property types for linked list node
Node should have a value and a next pointer which can be null if no next node.Step 2: Verify constructor initialization
Constructor sets value and initializes next to null (no next node initially).Final Answer:
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } -> Option AQuick Check:
Node next can be null [OK]
- Not allowing next to be null
- Using undefined instead of null
- Mismatching property names
pop() once?class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
class LinkedList {
head: Node | null = null;
append(value: number) {
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 && current.next.next) {
current = current.next;
}
const val = current.next!.value;
current.next = null;
return val;
}
print() {
let current = this.head;
let result = '';
while (current) {
result += current.value + ' -> ';
current = current.next;
}
result += 'null';
console.log(result);
}
}
const list = new LinkedList();
list.append(10);
list.append(20);
list.append(30);
list.pop();
list.print();Solution
Step 1: Understand list before pop
List after appending 10, 20, 30 is: 10 -> 20 -> 30 -> null.Step 2: Trace pop operation
Pop removes last node (30), so list becomes 10 -> 20 -> null.Final Answer:
10 -> 20 -> null -> Option BQuick Check:
Pop removes last node [OK]
- Assuming pop removes first node
- Not updating next pointer to null
- Printing old list without pop effect
pop() method for a singly linked list and select the correct fix.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 loop behavior
The loop moves current to the last node, but we need to stop at second last to remove last node.Step 2: Fix removal of last node
Set current.next = null at second last node to remove last node properly.Final Answer:
The loop should stop at the second last node; set current.next = null to remove last node -> Option CQuick Check:
Stop at second last node to remove last [OK]
- Setting current = null does not remove node
- Loop goes to last node instead of second last
- Returning wrong node value
pop() method that removes the last node and returns its value. However, if the list has only one node, pop() should remove it and set the list to empty. What will be the state of the list after calling pop() twice?Choose the correct printed output after these operations:
const list = new LinkedList(); list.append(5); list.append(15); list.append(25); list.append(35); list.pop(); list.pop(); list.print();
Solution
Step 1: Initial list and first pop
List starts as 5 -> 15 -> 25 -> 35 -> null. First pop removes 35, list becomes 5 -> 15 -> 25 -> null.Step 2: Second pop removes last node 25
After second pop, list is 5 -> 15 -> null.Final Answer:
5 -> 15 -> null -> Option DQuick Check:
Two pops remove last two nodes [OK]
- Removing nodes from front instead of end
- Not updating list head when one node left
- Assuming pop removes all nodes
Ready to prove it?
Pass the test and this pattern moves unknown
