Delete Node at End in DSA Typescript - Time & Space Complexity
We want to know how the time to delete the last node in a linked list changes as the list grows.
How does the number of steps grow when the list gets longer?
Analyze the time complexity of the following code snippet.
function deleteNodeAtEnd(head: ListNode | null): ListNode | 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 to the second last node and cutting off the last.
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, roughly one less than the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 steps |
| 100 | About 99 steps |
| 1000 | About 999 steps |
Pattern observation: The steps grow roughly in a straight line with the number of nodes.
Time Complexity: O(n)
This means the time to delete the last node grows directly with the list size.
[X] Wrong: "Deleting the last node is always quick because it's just one step."
[OK] Correct: You must first find the second last node by walking through the list, which takes time proportional to the list length.
Understanding how operations grow with input size helps you explain your code's efficiency 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 operation
Deleting a node at the end means removing the last node in the linked list.Step 2: Identify the effect on the list
After removal, the second last node becomes the new last node.Final Answer:
Removes the last node from the 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
head?Solution
Step 1: Analyze code for deleting last node
if (!head) return null; let temp = head; while (temp.next && temp.next.next) { temp = temp.next; } temp.next = null; traverses to the second last node and sets its next to null, removing the last node.Step 2: Check other options
if (!head) return null; head = head.next; deletes the first node, C tries to set temp to null but does not affect list, D deletes entire list.Final Answer:
Traverse to second last node, set next to null -> Option BQuick Check:
Traverse to second last node, set next to null = A [OK]
- Setting temp to null does not remove node from list
- Deleting head instead of last node
- Not checking for empty or single-node list
function deleteAtEnd(head: Node | null): Node | null {
if (!head) return null;
if (!head.next) return null;
let temp = head;
while (temp.next && temp.next.next) {
temp = temp.next;
}
temp.next = null;
return head;
}Solution
Step 1: Understand the code logic
The code removes the last node by stopping at second last node and setting its next to null.Step 2: Apply to given list
Original list ends with 4. After deletion, 3's next becomes null, so list is 1 -> 2 -> 3 -> null.Final Answer:
1 -> 2 -> 3 -> null -> Option DQuick Check:
Last node removed, list ends at 3 [OK]
- Not updating second last node's next to null
- Returning null for multi-node list
- Deleting first node instead of last
function deleteAtEnd(head: Node | null): Node | null {
if (!head) return null;
let temp = head;
while (temp.next) {
temp = temp.next;
}
temp = null;
return head;
}Solution
Step 1: Analyze the loop
The loop moves temp to the last node, but does not track second last node.Step 2: Check deletion step
Setting temp = null only changes local variable, does not remove last node from list.Final Answer:
It does not update the second last node's next pointer -> Option AQuick Check:
Must update second last node's next to null [OK]
- Setting temp to null does not remove node
- Not tracking second last node in loop
- Ignoring single-node list case
Solution
Step 1: Check handling of single-node list
function deleteAtEnd(head: Node | null): Node | null { if (!head) return null; if (!head.next) return null; let temp = head; while (temp.next && temp.next.next) { temp = temp.next; } temp.next = null; return head; } returns null if only one node exists, correctly deleting it.Step 2: Verify deletion of last node for longer lists
For longer lists, it traverses to the second last node using while (temp.next && temp.next.next) and sets temp.next = null, correctly removing the last node.Final Answer:
Correctly handles single-node by returning null -> Option AQuick Check:
Return null for single node, else remove last node [OK]
- Not returning null for single-node list
- Setting temp to null instead of updating next pointer
- Incorrect loop condition causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
