Java LinkedList getLast() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, getLast() returns the last element (the tail) of the LinkedList and leaves it in place. Because the list keeps a direct link to its tail, this is instant, however long the list is.

The method has no index to calculate, which is why list.getLast() is nicer than list.get(list.size() - 1). On an empty list it throws NoSuchElementException.

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 you expect to have elements.

Java
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.add("B");
list.add("C");
String tail = list.getLast();   // "C"
CallWhat it does
list.getLast()Returns the tail. Throws if the list is empty.
list.peekLast()Returns the tail, or null if the list is empty.
list.get(list.size() - 1)Same element, but needs an index and can throw IndexOutOfBoundsException.
list.getFirst()Reads the other end.

getLast only looks. To read the tail and remove it, use removeLast() or pollLast().

Library declarations
Java
// java.util.LinkedList
public E getLast()

Parameters

ParameterDescription
NonegetLast() takes no parameters.

Return Value

Return ValueDescription
EThe last element of the list.

Compare with Similar Methods

Ways to read the last element:

MethodHow it differs
peekLast()Returns null instead of throwing when the list is empty.
removeLast()Returns the last element and also removes it.
getFirst()The same operation for the head.
ArrayList.getLast()The ArrayList version, available from Java 21.

Java Examples

Example 1 — Read the Last Element of a List

Read the most recent entry of a log and compare the two ends.

Java
import java.util.LinkedList;

public class LinkedListGetLastDemo {
    public static void main(String[] args) {
        LinkedList<String> log = new LinkedList<>();
        log.add("server started");
        log.add("user logged in");
        log.add("file saved");

        System.out.println("latest: " + log.getLast());
        System.out.println("oldest: " + log.getFirst());
        System.out.println("entries: " + log.size());
    }
}

How It Works

  1. Entries are appended. Each add places a new message after the previous one.
  2. getLast gives the newest. The tail of the list is "file saved", the entry added last.
  3. getFirst gives the oldest. The head is "server started".
  4. Reading does not remove. The log still holds three entries.

Example 2 — Compare getLast with get(size() - 1)

Read the last element both ways and show what each one does on an empty list.

Java
import java.util.LinkedList;
import java.util.NoSuchElementException;

public class LinkedListGetLastVersus {
    public static void main(String[] args) {
        LinkedList<Integer> temps = new LinkedList<>();
        temps.add(18);
        temps.add(21);
        temps.add(19);

        System.out.println("getLast:         " + temps.getLast());
        System.out.println("get(size() - 1): " + temps.get(temps.size() - 1));

        temps.clear();
        try {
            temps.getLast();
        } catch (NoSuchElementException e) {
            System.out.println("empty getLast -> NoSuchElementException");
        }
        try {
            temps.get(temps.size() - 1);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("empty get(-1) -> IndexOutOfBoundsException");
        }
    }
}

How It Works

  1. Same answer on a full list. Both ways return 19, the last temperature.
  2. getLast is shorter. It needs no size and no subtraction, so there is no room for an off-by-one mistake.
  3. Empty list, getLast. After clear() there is no tail, so getLast() throws NoSuchElementException.
  4. Empty list, get(-1). The size is 0, so the index becomes -1, and get throws IndexOutOfBoundsException instead.

Example 3 — Use peekLast When the List May Be Empty

Print the last message of a chat that may be empty, using peekLast and a null check.

Java
import java.util.LinkedList;

public class LinkedListGetLastSafe {
    static void showLast(LinkedList<String> chat) {
        String last = chat.peekLast();
        if (last == null) {
            System.out.println("no messages yet");
        } else {
            System.out.println("last message: " + last);
        }
    }

    public static void main(String[] args) {
        LinkedList<String> chat = new LinkedList<>();
        showLast(chat);

        chat.add("Hello!");
        chat.add("Are you there?");
        showLast(chat);
    }
}

How It Works

  1. A helper that expects empty lists. It calls peekLast(), which gives null when nothing is stored.
  2. First call. The chat is empty, so the helper prints "no messages yet".
  3. Two messages added. The tail is now "Are you there?".
  4. Second call. The helper prints the tail. No exception handling was needed anywhere.

Frequently Asked Questions

Call list.getLast(). It returns the last element and keeps it in the list.
It throws a NoSuchElementException. Use peekLast() to get null instead.
Yes. A LinkedList keeps a direct link to its last node, so getLast runs in constant time.
Both return the last element. On an empty list getLast throws NoSuchElementException, and peekLast returns null.
Use getLast(). It is clearer, and it avoids the index arithmetic.

Next: indexOf()

Find the index of the first matching element, or -1.

indexOf() 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.

10 people found this page helpful