Delete Node at End in DSA Java - 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 longer?
Analyze the time complexity of the following code snippet.
public void deleteAtEnd() {
if (head == null) return;
if (head.next == null) {
head = null;
return;
}
Node current = head;
while (current.next.next != null) {
current = current.next;
}
current.next = null;
}
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 nodes to find the second last node.
- How many times: It runs once for each node except the last two, so roughly n-2 times for a list of size n.
As the list grows, the loop runs more times because it must reach near the end.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 8 times through the loop |
| 100 | About 98 times through the loop |
| 1000 | About 998 times through the loop |
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 takes constant time because we just remove it."
[OK] Correct: We must first find the second last node by walking through the list, which takes time proportional to the list length.
Understanding how linked list operations scale helps you explain your code clearly and shows you know how data structures behave in real situations.
"What if we kept a pointer to the last node? How would the time complexity of deleting the last node change?"
Practice
Delete Node at End do in a singly linked list?Solution
Step 1: Understand the operation meaning
Deleting a node at the end means removing the last node in the linked list.Step 2: Identify the correct description
Removing the last node matches Removes the last node from the list, while others describe adding or removing nodes at different positions.Final Answer:
Removes the last node from the list -> Option AQuick Check:
Delete Node at End = Removes last node [OK]
- Confusing delete at end with delete at beginning
- Thinking it adds a node instead of removing
- Mixing up first and last node operations
list assuming you have a method deleteAtEnd()?Solution
Step 1: Identify method call syntax in Java
Calling a method on an object uses dot notation with parentheses: object.methodName();Step 2: Match correct syntax
list.deleteAtEnd(); uses correct syntax: list.deleteAtEnd(); others have syntax errors or wrong usage.Final Answer:
list.deleteAtEnd(); -> Option AQuick Check:
Method call syntax = object.method() [OK]
- Omitting parentheses when calling methods
- Using brackets [] instead of parentheses ()
- Reversing object and method names
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
class LinkedList {
Node head;
void deleteAtEnd() {
if (head == null) return;
if (head.next == null) {
head = null;
return;
}
Node temp = head;
while (temp.next.next != null) {
temp = temp.next;
}
temp.next = null;
}
void printList() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("null");
}
}
public class Main {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.head = new Node(1);
list.head.next = new Node(2);
list.head.next.next = new Node(3);
list.deleteAtEnd();
list.printList();
}
}
What is the output after running this code?Solution
Step 1: Understand initial list and deleteAtEnd()
The list starts as 1 -> 2 -> 3 -> null. The deleteAtEnd() removes the last node (3).Step 2: Trace deleteAtEnd() method
It traverses to node with data 2, sets its next to null, removing node 3.Final Answer:
1 -> 2 -> null -> Option BQuick Check:
Last node removed, list ends at 2 [OK]
- Forgetting to update the second last node's next to null
- Assuming head changes when list has multiple nodes
- Printing old list without deletion
void deleteAtEnd() {
if (head == null) return;
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp = null;
}Solution
Step 1: Analyze the loop and pointer updates
The loop moves temp to the last node (where temp.next is null). Setting temp = null only changes local pointer, not list links.Step 2: Identify missing update to second last node
The second last node's next pointer is not set to null, so the last node remains linked.Final Answer:
It does not update the second last node's next pointer -> Option DQuick Check:
Must update second last node's next to null [OK]
- Setting temp = null only changes local variable
- Not handling single-node list case
- Ignoring list head update if only one node
Solution
Step 1: Delete the last node
Calling deleteAtEnd() removes node with value 40, list becomes 10 -> 20 -> 30 -> null.Step 2: Add new node with value 50 at end
Calling addAtEnd(50) appends node 50, list becomes 10 -> 20 -> 30 -> 50 -> null.Final Answer:
Call deleteAtEnd(), then call addAtEnd(50) -> Option CQuick Check:
Delete last, then add new node [OK]
- Adding new node before deleting old last node
- Deleting twice unnecessarily
- Not deleting at all before adding
Ready to prove it?
Pass the test and this pattern moves unknown
