Java LinkedList removeLast() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

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.

Java
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
list.add("C");
String gone = list.removeLast();   // "C", list is now [A, B]
CallWhat 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
// java.util.LinkedList
public E removeLast()

Parameters

ParameterDescription
NoneremoveLast() takes no parameters.

Return Value

Return ValueDescription
EThe element that used to be last.

Compare with Similar Methods

Ways to take the last element:

MethodHow it differs
pollLast()Returns null instead of throwing when the list is empty.
getLast()Reads the last element without removing it.
removeFirst()The same operation at the other end.
ArrayList.removeLast()The ArrayList version, available from Java 21.

Java Examples

Example 1 — Remove the Last Item from a List

Take the last item off a list twice and print what was removed.

Java
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());
    }
}

How It Works

  1. A route of four stops. "Airport" is the last stop.
  2. First removal. removeLast() returns "Airport" and shortens the route.
  3. Second removal. "Harbor" is now the last stop, so it is returned and removed next.
  4. 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.

Java
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);
    }
}

How It Works

  1. Newest page is last. Visiting a page adds it at the end, so the current page is getLast().
  2. Going back removes the tail. After the first removeLast(), "shop" is the current page.
  3. Back again. Removing "shop" leaves "home" as the current page.
  4. History shrinks. Only "home" remains in the list.

Example 3 — Protect removeLast from an Empty List

Compare removeLast with pollLast on an empty list.

Java
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());
        }
    }
}

How It Works

  1. An empty list. There is no tail.
  2. pollLast gives null. No exception is thrown, and null is printed.
  3. removeLast gives an exception. It throws NoSuchElementException, which is caught.
  4. A guard avoids the problem. With 7 in the list, isEmpty() is false, so removeLast() safely returns 7.

Frequently Asked Questions

Call list.removeLast(). It deletes the last element and returns it.
It throws a NoSuchElementException. Use pollLast() to get null instead.
Yes. A LinkedList is doubly linked and keeps a link to its tail, so removing the last element takes constant time.
Both remove and return the last element. On an empty list removeLast throws NoSuchElementException and pollLast returns null.
No. Changing the list during iteration causes a ConcurrentModificationException. Use an iterator or a while loop instead.

Next: removeLastOccurrence()

Remove the last element equal to a value, searching from the tail.

removeLastOccurrence() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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