Delete Node at End in DSA Javascript - Time & Space Complexity
We want to understand how long it takes to remove the last node from a linked list.
How does the time needed change as the list gets bigger?
Analyze the time complexity of the following code snippet.
function deleteAtEnd(head) {
if (!head) return null;
if (!head.next) return null;
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 through the list to find the second last node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through the list nodes.
- How many times: It runs once for each node until the second last node.
As the list gets longer, the loop runs more times, moving through each node one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 steps through nodes |
| 100 | About 99 steps through nodes |
| 1000 | About 999 steps through nodes |
Pattern observation: The number of steps grows roughly the same as the list size.
Time Complexity: O(n)
This means the time to delete the last node grows linearly with the list size.
[X] Wrong: "Deleting the last node is always quick because it's just one node."
[OK] Correct: We must find the node before the last one, which takes time proportional to the list length.
Knowing how deleting the last node scales helps you explain linked list operations clearly and confidently.
"What if the list had a tail pointer that points directly to the last node? How would the time complexity change?"
Practice
Delete Node at End do in a singly linked list?Solution
Step 1: Understand the linked list structure
A singly linked list is a chain of nodes where each node points to the next node, ending with a node pointing to null.Step 2: Identify what deleting the last node means
Deleting the last node means removing the node that points to null, effectively shortening the list by one node at the end.Final Answer:
Removes the last node from the linked list -> Option CQuick Check:
Delete Node at End = Remove last node [OK]
- Confusing delete at end with delete at start
- Thinking it adds a node instead of removing
- Assuming it deletes a middle node
Solution
Step 1: Recognize common JavaScript linked list methods
JavaScript arrays usepop()to remove the last element; custom linked lists often implement similar methods.Step 2: Identify the standard method name
Among the options,pop()is the standard method name used to remove the last element in many list implementations.Final Answer:
list.pop(); -> Option BQuick Check:
pop() removes last element [OK]
- Using non-standard method names
- Confusing delete with remove
- Assuming deleteEnd() is built-in
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
deleteAtEnd() {
if (!this.head) return null;
if (!this.head.next) {
this.head = null;
return;
}
let current = this.head;
while (current.next.next) {
current = current.next;
}
current.next = null;
}
}
let list = new LinkedList();
list.head = new Node(1);
list.head.next = new Node(2);
list.head.next.next = new Node(3);
list.deleteAtEnd();
let result = [];
let temp = list.head;
while(temp) {
result.push(temp.data);
temp = temp.next;
}
console.log(result);What will be printed?
Solution
Step 1: Understand the initial list and deletion
The list starts as 1 -> 2 -> 3. ThedeleteAtEnd()method removes the last node (3).Step 2: Trace the deletion and final list
After deletion, the list becomes 1 -> 2. The loop collects these values into the array [1, 2].Final Answer:
[1, 2] -> Option DQuick Check:
Deleting last node removes 3, leaving [1, 2] [OK]
- Forgetting to update the second last node's next to null
- Assuming the head changes incorrectly
- Not handling single-node list case
deleteAtEnd() {
let current = this.head;
while (current.next) {
current = current.next;
}
current = null;
}What is the main problem?
Solution
Step 1: Analyze the loop and pointer update
The loop movescurrentto the last node, but settingcurrent = nullonly changes the local variable, not the list's links.Step 2: Understand why the last node is not removed
To remove the last node, the previous node'snextmust be set to null. This code misses that step.Final Answer:
It sets the local variable to null but does not update the list -> Option AQuick Check:
Local variable null doesn't remove node [OK]
- Setting current to null instead of previous.next
- Not checking for single-node list
- Ignoring empty list case
Solution
Step 1: Delete the last node
CallingdeleteAtEnd()removes the node with value 40, list becomes 10 -> 20 -> 30.Step 2: Add new node at the end
CallingaddAtEnd(50)adds a new node with value 50 at the end, list becomes 10 -> 20 -> 30 -> 50.Final Answer:
Call deleteAtEnd(), then call addAtEnd(50) -> Option AQuick Check:
Delete last then add new last node [OK]
- Adding before deleting causes wrong order
- Deleting twice removes too many nodes
- Adding at start changes list order
Ready to prove it?
Pass the test and this pattern moves unknown
