Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java LinkedList addLast: Append Elements to the Tail

How to use Java's LinkedList.addLast() to append elements to the tail, including syntax, O(1) performance behavior, null edge cases, and comparisons with add() and offerLast().

~3 min readJava
#LinkedList#Java Collections#Deque#addLast
Illustration of a Java LinkedList node being appended at the tail of a doubly linked list chain

Appending an element to the end of a Java LinkedList is done with the addLast(E e) method. For a developer searching for java linkedlist addlast, the practical questions are: what does the method return, what is its runtime cost, and when should it be preferred over add() or offerLast()? This article answers those questions with concrete examples and explains the behavior you can rely on.

Basic Syntax of addLast

The method signature is simple: addLast(E e) takes a single argument of the list's element type and returns void. Here is the minimal usage:

java
LinkedList<String> tasks = new LinkedList<>(); tasks.addLast("compile"); tasks.addLast("test"); tasks.addLast("deploy");

Each call creates a new node, links it to the current tail node, and updates the internal tail reference. When the list is empty, the new node becomes both the head and the tail. The following example shows that behavior:

java
LinkedList<Integer> numbers = new LinkedList<>(); numbers.addLast(42); System.out.println(numbers.getFirst()); // 42 System.out.println(numbers.getLast()); // 42

After the first addLast call, both getFirst() and getLast() return the same element because the list contains exactly one node.

How addLast Compares With add and offerLast

LinkedList implements both List and Deque, so several methods can append an element to the end. The choice matters when you care about return values and interface consistency.

MethodReturnsInterfaceBehavior
addLast(E)voidDequeAppends to tail
add(E)booleanListAppends to tail, always returns true
offerLast(E)booleanDequeAppends to tail, returns true on success

All three methods perform the same underlying operation on a LinkedList. The difference is the return type. addLast signals that you are treating the collection as a deque and do not need a status value. offerLast is useful when the implementing collection has a capacity limit, although LinkedList itself is unbounded.

Type Safety With Generics

When you declare a LinkedList with a specific element type, addLast enforces that type at compile time:

java
LinkedList<UUID> ids = new LinkedList<>(); ids.addLast(UUID.randomUUID());

Attempting to pass an incompatible type produces a compile error rather than a runtime failure. This is the same type safety you get from add, but the explicit addLast name makes the operation's position clear when reading the code later.

Performance: Why addLast Is O(1)

The LinkedList implementation maintains references to both the first and last nodes. Calling addLast allocates a new node and links it at the end by updating the relevant previous and next references, then updates the tail reference. When the list is empty, the new node becomes both head and tail. No elements are shifted, and no array resizing occurs.

This is different from ArrayList, where appending to the end is amortized O(1) but can trigger a full array copy when the internal buffer is full. For LinkedList, each addLast call is O(1) regardless of list size.

The tradeoff is memory: each element in a LinkedList is wrapped in a node object that stores the element plus two pointers. ArrayList and ArrayDeque avoid that per-node overhead, so they can use less memory for the same data.

Edge Cases: Null Elements and Empty Lists

LinkedList permits null elements, so addLast(null) succeeds and stores a null reference in the list. This is different from ArrayDeque, which rejects null elements and throws NullPointerException. If you later iterate the list, you must handle the null case:

java
LinkedList<String> items = new LinkedList<>(); items.addLast(null); items.addLast("value"); for (String item : items) { if (item == null) { System.out.println("null element found"); } }

Calling addLast on an empty list is safe and simply creates the first node. There is no precondition that the list must already contain elements.

Choosing Between LinkedList and Other Collections

Use addLast on a LinkedList when you need a doubly linked list with constant-time insertion at both ends and are comfortable with the memory overhead of node objects. If you only need a FIFO queue and never need to remove from the middle, ArrayDeque is a better choice because it uses a resizable array and does not allocate a node per element. If you need random access by index, ArrayList is the appropriate structure, and addLast is not the right method to focus on because ArrayList does not implement Deque.

The decision comes down to access patterns. Frequent head and tail operations with occasional middle access favor LinkedList. Frequent indexed reads favor ArrayList. A strict queue with no null elements favors ArrayDeque.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.