Java LinkedList add() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, add(e) puts a new element at the end of a LinkedList. The second form, add(index, e), inserts the element at a position and moves the later elements one place along.

A LinkedList is a chain of small nodes, so adding at the end is fast and no big array has to grow. The list accepts duplicates and also accepts null.

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 List, and LinkedList implements it.

Syntax

Add to the end, or give an index first.

Java
LinkedList<String> list = new LinkedList<>();
list.add("A");          // [A]
list.add("C");          // [A, C]
list.add(1, "B");       // [A, B, C]
boolean ok = list.add("D"); // true
CallWhat it does
list.add(e)Appends e to the end and returns true.
list.add(index, e)Inserts e at index. Returns nothing.
list.addLast(e)Does the same as add(e), but returns nothing.
list.addFirst(e)Inserts at the front instead.

The one-argument form always returns true, because a LinkedList has no size limit. The two-argument form returns nothing.

Library declarations
Java
// java.util.LinkedList
public boolean add(E e)
public void add(int index, E element)

Parameters

ParameterDescription
e (E)The element to add. It may be null.
index (int)Where to insert, from 0 to size(). Only used by the two-argument form.

Return Value

Return ValueDescription
booleanFor add(e): always true.
voidFor add(index, e): nothing is returned.

Compare with Similar Methods

Ways to add an element:

MethodHow it differs
addLast(e)Same effect as add(e), but with a void return.
addFirst(e)Puts the element at the front of the list.
offer(e)The queue-style name for adding at the tail; also returns true.
ArrayList.add()The same method on an array-backed list; inserting at the end is also fast there.

Java Examples

Example 1 — Add Tasks to the End of a List

Add three tasks one by one, print the list, and check the value that add returns.

Java
import java.util.LinkedList;

public class LinkedListAddDemo {
    public static void main(String[] args) {
        LinkedList<String> tasks = new LinkedList<>();

        boolean first = tasks.add("Write report");
        tasks.add("Send email");
        tasks.add("Book meeting");

        System.out.println("tasks: " + tasks);
        System.out.println("add returned: " + first);
        System.out.println("size: " + tasks.size());
    }
}

How It Works

  1. Start empty. new LinkedList<>() makes a list with no elements.
  2. Each add goes to the back. "Write report" is added first, so it stays at the front, and "Book meeting" ends up last.
  3. The return value. add reports true, which is why first prints true.
  4. Three elements. After three calls the list holds three tasks, so size() is 3.

Example 2 — Insert a Song in the Middle of a Playlist

Use add(index, e) to squeeze a song into the middle and to place one at the very end.

Java
import java.util.LinkedList;

public class LinkedListAddIndex {
    public static void main(String[] args) {
        LinkedList<String> playlist = new LinkedList<>();
        playlist.add("Intro");
        playlist.add("Finale");
        System.out.println("before: " + playlist);

        playlist.add(1, "Chorus");
        System.out.println("add(1): " + playlist);

        playlist.add(0, "Opening");
        System.out.println("add(0): " + playlist);

        playlist.add(playlist.size(), "Encore");
        System.out.println("at end: " + playlist);
    }
}

How It Works

  1. Two songs to begin with. The playlist starts as Intro and Finale, at positions 0 and 1.
  2. Insert at 1. "Chorus" takes position 1, and "Finale" slides back to position 2.
  3. Insert at 0. Index 0 is the front, so "Opening" becomes the new first song and everything else moves back by one.
  4. Index equal to size. With four songs, size() is 4. Inserting there is legal and works like a plain append.

Example 3 — See What a LinkedList Allows When You Add

Add a duplicate and a null, then use an index that is too large and catch the exception.

Java
import java.util.LinkedList;

public class LinkedListAddRules {
    public static void main(String[] args) {
        LinkedList<String> names = new LinkedList<>();
        names.add("Ana");
        names.add("Ana");
        names.add(null);
        System.out.println("names: " + names);
        System.out.println("size: " + names.size());

        try {
            names.add(10, "Zed");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("add(10, ...) failed: " + e.getClass().getSimpleName());
        }
        System.out.println("after: " + names);
    }
}

How It Works

  1. Duplicates are fine. The list simply keeps both "Ana" entries, one after the other.
  2. null is allowed. A LinkedList stores null as a normal element, and it prints as the word null.
  3. Index 10 is out of range. The list has three elements, so only indexes 0 to 3 are valid, and Java throws IndexOutOfBoundsException.
  4. No change on failure. The failed call leaves the list exactly as it was, with three elements.

Frequently Asked Questions

Call list.add(element) to append it to the end. Call list.add(index, element) to insert it at a position.
The one-argument add(e) always returns true. The two-argument add(index, e) returns nothing (void).
Yes. A LinkedList accepts both null and repeated values, unlike a Set.
They do the same thing: both append to the end. add returns true, while addLast returns nothing.
The index must be between 0 and size() inclusive. A negative index, or one greater than size(), throws the exception.

Next: addAll()

Append every element of another collection, or insert them at an index.

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

7 people found this page helpful