Home Java LinkedList Methods removeLast() Java LinkedList removeLast() Method Definition In Java, removeLast() takes the last element (the tail) out of the LinkedList and returns it. The element before it becomes the new tail.
Because a LinkedList is doubly linked, it can step back from the tail in one move, so this is as fast as removing from the front. An ArrayList is fast here too, but a singly linked list would not be.
An instance method of java.util.LinkedList (since Java 1.2): call it on a LinkedList object. Add import java.util.LinkedList; at the top of your file. It is declared by Deque, and LinkedList implements it. Call it on a LinkedList (or Deque) variable: a plain List variable cannot see it.
Syntax Call it on a list that is not empty.
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
list.add("C");
String gone = list.removeLast(); // "C", list is now [A, B] Call What it does list.removeLast()Removes and returns the tail. Throws if empty. list.pollLast()Removes and returns the tail, or null if empty. list.getLast()Reads the tail without removing it. list.removeFirst()Removes from the head instead.
The removed element is returned, so you can keep it in a variable.
Library declarations // java.util.LinkedList
public E removeLast() Parameters Parameter Description None removeLast() takes no parameters.
Return Value Return Value Description EThe element that used to be last.
Compare with Similar Methods Ways to take the last element:
Java Examples 1. Remove from the back 2. Browser back button 3. Empty list Example 1 — Remove the Last Item from a List Take the last item off a list twice and print what was removed.
import java.util.LinkedList;
public class LinkedListRemoveLastDemo {
public static void main(String[] args) {
LinkedList<String> stops = new LinkedList<>();
stops.add("Central");
stops.add("Market");
stops.add("Harbor");
stops.add("Airport");
System.out.println("removed: " + stops.removeLast());
System.out.println("removed: " + stops.removeLast());
System.out.println("route: " + stops);
System.out.println("last stop now: " + stops.getLast());
}
} removed: Airport
removed: Harbor
route: [Central, Market]
last stop now: Market How It Works A route of four stops. "Airport" is the last stop.First removal. removeLast() returns "Airport" and shortens the route.Second removal. "Harbor" is now the last stop, so it is returned and removed next.A new tail. The route has two stops left, and getLast() reports "Market".Example 2 — Build a Back Button with removeLast Keep visited pages in a list, and go back by removing the newest page.
import java.util.LinkedList;
public class LinkedListRemoveLastBack {
public static void main(String[] args) {
LinkedList<String> pages = new LinkedList<>();
pages.add("home");
pages.add("shop");
pages.add("cart");
System.out.println("now on: " + pages.getLast());
pages.removeLast();
System.out.println("back to: " + pages.getLast());
pages.removeLast();
System.out.println("back to: " + pages.getLast());
System.out.println("history: " + pages);
}
} now on: cart
back to: shop
back to: home
history: [home] How It Works Newest page is last. Visiting a page adds it at the end, so the current page is getLast().Going back removes the tail. After the first removeLast(), "shop" is the current page.Back again. Removing "shop" leaves "home" as the current page.History shrinks. Only "home" remains in the list.Example 3 — Protect removeLast from an Empty List Compare removeLast with pollLast on an empty list.
import java.util.LinkedList;
import java.util.NoSuchElementException;
public class LinkedListRemoveLastEmpty {
public static void main(String[] args) {
LinkedList<Integer> stack = new LinkedList<>();
System.out.println("pollLast: " + stack.pollLast());
try {
stack.removeLast();
} catch (NoSuchElementException e) {
System.out.println("removeLast -> NoSuchElementException");
}
stack.add(7);
if (!stack.isEmpty()) {
System.out.println("guarded removeLast: " + stack.removeLast());
}
}
} pollLast: null
removeLast -> NoSuchElementException
guarded removeLast: 7 How It Works An empty list. There is no tail.pollLast gives null. No exception is thrown, and null is printed.removeLast gives an exception. It throws NoSuchElementException, which is caught.A guard avoids the problem. With 7 in the list, isEmpty() is false, so removeLast() safely returns 7.Frequently Asked Questions How do I remove the last element of a LinkedList in Java? Call list.removeLast(). It deletes the last element and returns it.
What happens if removeLast() is called on an empty list? It throws a NoSuchElementException. Use pollLast() to get null instead.
Is removeLast() fast on a LinkedList? Yes. A LinkedList is doubly linked and keeps a link to its tail, so removing the last element takes constant time.
What is the difference between removeLast() and pollLast()? Both remove and return the last element. On an empty list removeLast throws NoSuchElementException and pollLast returns null.
Can I use removeLast() inside a for-each loop over the same list? No. Changing the list during iteration causes a ConcurrentModificationException. Use an iterator or a while loop instead.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful
Helpful Share Copy link Suggestion