Java LinkedList offerLast() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, offerLast(e) inserts the element at the end of the LinkedList and returns true. It is the Deque method for adding at the tail.

For a LinkedList it behaves exactly like add(e), addLast(e) and offer(e). Only the name and the return type differ, so pick the one that matches the interface you are coding against.

An instance method of java.util.LinkedList (since Java 1.6): 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

Pass the element for the tail position.

Java
Deque<String> deque = new LinkedList<>();
deque.offerLast("A");          // true, [A]
deque.offerLast("B");          // true, [A, B]
String last = deque.peekLast(); // "B"
CallWhat it does
deque.offerLast(e)Inserts e at the tail and returns true.
deque.addLast(e)Same effect, with no return value.
deque.offer(e)The Queue name for the same action.
deque.offerFirst(e)Inserts at the head instead.

The result is always true for a LinkedList. It would be false only for a size-limited deque that is full.

Library declarations
Java
// java.util.LinkedList
public boolean offerLast(E e)

Parameters

ParameterDescription
e (E)The element to append at the tail. It may be null.

Return Value

Return ValueDescription
booleantrue, always, for a LinkedList.

Compare with Similar Methods

Ways to insert at the tail:

MethodHow it differs
addLast(e)Same action without a return value.
offer(e)The queue name for the same action.
offerFirst(e)The matching call for the head.
ArrayDeque.offerLast()The array-based version, which rejects null.

Java Examples

Example 1 — Offer Messages to the End of a Deque

Add three chat messages to the tail and look at both ends.

Java
import java.util.Deque;
import java.util.LinkedList;

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

        boolean ok = chat.offerLast("Hi");
        chat.offerLast("How are you?");
        chat.offerLast("See you soon");

        System.out.println("chat: " + chat);
        System.out.println("ok: " + ok);
        System.out.println("oldest: " + chat.peekFirst());
        System.out.println("newest: " + chat.peekLast());
    }
}

How It Works

  1. Messages go to the tail. "Hi" is stored first, and each new message is added after the previous one.
  2. Reading order is arrival order. The printed list reads from the oldest message to the newest.
  3. A true result. ok holds the result of the first offer, which is true.
  4. Both ends are visible. peekFirst() is the oldest message and peekLast() is the newest.

Example 2 — Keep Only the Last Three Readings

Offer sensor readings to the tail and drop the oldest from the head whenever the list passes three.

Java
import java.util.Deque;
import java.util.LinkedList;

public class LinkedListOfferLastWindow {
    public static void main(String[] args) {
        Deque<Integer> window = new LinkedList<>();
        int[] readings = {21, 23, 22, 25, 27};

        for (int value : readings) {
            window.offerLast(value);
            if (window.size() > 3) {
                window.pollFirst();
            }
            System.out.println("after " + value + ": " + window);
        }
    }
}

How It Works

  1. Newest reading goes last. Each value is placed at the tail with offerLast.
  2. Up to three are kept. The first three readings simply pile up, so nothing is dropped yet.
  3. The fourth triggers a drop. When 25 arrives the size is 4, so pollFirst() removes the oldest reading, 21.
  4. A sliding window. The list always shows the three latest readings, and the last line ends with 22, 25, 27.

Example 3 — Build a List with offerFirst and offerLast

Offer names to both ends of one deque and read the final order.

Java
import java.util.Deque;
import java.util.LinkedList;

public class LinkedListOfferLastMix {
    public static void main(String[] args) {
        Deque<String> deque = new LinkedList<>();

        deque.offerLast("B");
        deque.offerLast("C");
        deque.offerFirst("A");
        deque.offerLast("D");
        deque.offerFirst("Z");

        System.out.println(deque);
        System.out.println("size: " + deque.size());
    }
}

How It Works

  1. Start at the tail. Two tail offers give B, C.
  2. A joins at the head. offerFirst("A") puts A in front of B, so the list is A, B, C.
  3. D joins at the tail. The list becomes A, B, C, D.
  4. Z goes to the very front. The final list reads Z, A, B, C, D, which is five elements.

Frequently Asked Questions

It inserts the element at the end (tail) of the list and returns true.
Yes, the effect is identical. Both append to the end and both return true. offerLast is the name used by the Deque interface.
Not on a LinkedList. It has no capacity limit, so the result is always true.
offerLast returns true, and addLast returns nothing. On a bounded deque, offerLast returns false when full, while addLast throws an exception.
Use offerLast when the variable is a Deque and you also use offerFirst, so the code clearly reads as two-ended.

Next: peek()

Look at the head without removing it; null if empty.

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

9 people found this page helpful