Java LinkedList addLast: Append Elements to the Tail
java linkedlist addlast: Learn how to use addLast() on Java LinkedList to append elements at the tail, with syntax, performance behavior, and comparison with add() and...
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:
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:
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.
| Method | Returns | Interface | Behavior |
|---|---|---|---|
addLast(E) | void | Deque | Appends to tail |
add(E) | boolean | List | Appends to tail, always returns true |
offerLast(E) | boolean | Deque | Appends 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:
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 performs three steps: allocate a new node, set the new node's previous pointer to the current tail, and update the tail reference. 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, every addLast call has the same constant cost regardless of the list size.
The tradeoff is memory: each element in a LinkedList is wrapped in a node object that stores the element plus two pointers. If you are appending a large number of elements and do not need removal from the head, ArrayList or ArrayDeque will use less memory.
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:
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.