Home Java LinkedList Methods offerLast() Java LinkedList offerLast() Method 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.
Deque<String> deque = new LinkedList<>();
deque.offerLast("A"); // true, [A]
deque.offerLast("B"); // true, [A, B]
String last = deque.peekLast(); // "B" Call What 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.util.LinkedList
public boolean offerLast(E e) Parameters Parameter Description e (E)The element to append at the tail. It may be null.
Return Value Return Value Description booleantrue, always, for a LinkedList.
Compare with Similar Methods Ways to insert at the tail:
Java Examples 1. Append to a deque 2. Keep the last N 3. Mix with offerFirst Example 1 — Offer Messages to the End of a Deque Add three chat messages to the tail and look at both ends.
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());
}
} chat: [Hi, How are you?, See you soon]
ok: true
oldest: Hi
newest: See you soon How It Works Messages go to the tail. "Hi" is stored first, and each new message is added after the previous one.Reading order is arrival order. The printed list reads from the oldest message to the newest.A true result. ok holds the result of the first offer, which is true.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.
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);
}
}
} after 21: [21]
after 23: [21, 23]
after 22: [21, 23, 22]
after 25: [23, 22, 25]
after 27: [22, 25, 27] How It Works Newest reading goes last. Each value is placed at the tail with offerLast.Up to three are kept. The first three readings simply pile up, so nothing is dropped yet.The fourth triggers a drop. When 25 arrives the size is 4, so pollFirst() removes the oldest reading, 21.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.
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 Start at the tail. Two tail offers give B, C.A joins at the head. offerFirst("A") puts A in front of B, so the list is A, B, C.D joins at the tail. The list becomes A, B, C, D.Z goes to the very front. The final list reads Z, A, B, C, D, which is five elements.Frequently Asked Questions What does LinkedList offerLast() do? It inserts the element at the end (tail) of the list and returns true.
Is offerLast() the same as add() on a LinkedList? Yes, the effect is identical. Both append to the end and both return true. offerLast is the name used by the Deque interface.
Can offerLast() return false? Not on a LinkedList. It has no capacity limit, so the result is always true.
What is the difference between offerLast() and addLast()? offerLast returns true, and addLast returns nothing. On a bounded deque, offerLast returns false when full, while addLast throws an exception.
When should I use offerLast() instead of add()? Use offerLast when the variable is a Deque and you also use offerFirst, so the code clearly reads as two-ended.
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.
9 people found this page helpful
Helpful Share Copy link Suggestion