Java LinkedList remove() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, remove() has three forms. With no argument it removes and returns the head of the LinkedList. With an int index it removes and returns the element at that position. With an object it removes the first matching element and says whether it found one.

After a removal, the elements after the removed position move up by one, so the list becomes one shorter. A LinkedList does this by relinking two nodes, not by copying elements.

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

Syntax

Three forms, chosen by the argument you pass.

Java
LinkedList<String> list = new LinkedList<>();
list.add("A"); list.add("B"); list.add("C"); list.add("B");

String head = list.remove();            // "A"  -> [B, C, B]
String at1 = list.remove(1);            // "C"  -> [B, B]
boolean gone = list.remove("B");        // true -> [B]
CallWhat it does
list.remove()Removes and returns the head. Throws if the list is empty.
list.remove(index)Removes and returns the element at index.
list.remove(obj)Removes the first element equal to obj. Returns true if one was removed.
list.removeFirstOccurrence(obj)The deque name for removing the first match.

The result type depends on the form: an element for the first two, and a boolean for the object form.

Library declarations
Java
// java.util.LinkedList
public E remove()
public E remove(int index)
public boolean remove(Object o)

Parameters

ParameterDescription
index (int)The position to remove, from 0 to size() - 1.
o (Object)The element to look for. Compared with equals.

Return Value

Return ValueDescription
EFor remove() and remove(index): the removed element.
booleanFor remove(o): true if an element was removed, false if there was no match.

Compare with Similar Methods

Related ways to remove an element:

MethodHow it differs
removeFirst()The same as remove(), named for the deque.
poll()Removes the head, but returns null instead of throwing when empty.
removeFirstOccurrence(o)Same as remove(o), under the deque name.
ArrayList.remove()The same three ideas on an array-backed list; removing from the middle shifts elements.

Java Examples

Example 1 — Remove by Head, Index and Value

Use all three forms of remove on the same list and print the result of each.

Java
import java.util.LinkedList;

public class LinkedListRemoveForms {
    public static void main(String[] args) {
        LinkedList<String> letters = new LinkedList<>();
        letters.add("A");
        letters.add("B");
        letters.add("C");
        letters.add("B");
        System.out.println("start: " + letters);

        String head = letters.remove();
        System.out.println("remove(): " + head + " -> " + letters);

        String atOne = letters.remove(1);
        System.out.println("remove(1): " + atOne + " -> " + letters);

        boolean found = letters.remove("B");
        System.out.println("remove(\"B\"): " + found + " -> " + letters);
    }
}

How It Works

  1. Four letters to start. The list has A, B, C and B, so "B" appears twice.
  2. No argument removes the head. "A" is taken off the front and returned.
  3. An int removes by position. In the list B, C, B, index 1 holds "C", so "C" is removed and returned.
  4. An object removes the first match. The list is now B, B. The first "B" is deleted, the call returns true, and one "B" is left.

Example 2 — Avoid the remove(int) and remove(Object) Trap

Remove a number from a list of numbers by index and by value, and see why the two calls differ.

Java
import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListRemoveTrap {
    public static void main(String[] args) {
        LinkedList<Integer> byIndex = new LinkedList<>(Arrays.asList(10, 20, 30, 2));
        byIndex.remove(2);
        System.out.println("remove(2):                    " + byIndex);

        LinkedList<Integer> byValue = new LinkedList<>(Arrays.asList(10, 20, 30, 2));
        byValue.remove(Integer.valueOf(2));
        System.out.println("remove(Integer.valueOf(2)):   " + byValue);

        LinkedList<Integer> missing = new LinkedList<>(Arrays.asList(10, 20, 30));
        System.out.println("remove value 99: " + missing.remove(Integer.valueOf(99)));
    }
}

How It Works

  1. Same starting numbers. Each list begins as 10, 20, 30, 2, so the value 2 sits at index 3.
  2. remove(2) uses an index. A plain int means a position, so the element at index 2, the number 30, is removed.
  3. valueOf forces the value form. Integer.valueOf(2) is an object, so the list removes the number 2 itself.
  4. No match returns false. There is no 99 in the third list, so nothing is removed and the call returns false.

Example 3 — Handle the Exceptions That remove Can Throw

Try an empty list, a bad index, and a missing value, and catch each problem.

Java
import java.util.LinkedList;
import java.util.NoSuchElementException;

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

        try {
            tasks.remove();
        } catch (NoSuchElementException e) {
            System.out.println("remove() on empty -> NoSuchElementException");
        }

        tasks.add("wash car");
        try {
            tasks.remove(5);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("remove(5) -> IndexOutOfBoundsException");
        }

        System.out.println("remove(\"walk dog\"): " + tasks.remove("walk dog"));
        System.out.println("tasks: " + tasks);
    }
}

How It Works

  1. Nothing to remove. The no-argument form needs a head, so on an empty list it throws NoSuchElementException.
  2. One task added. The list now has a single element at index 0.
  3. Index 5 does not exist. The index form throws IndexOutOfBoundsException for any position outside 0 to 0.
  4. A missing object is not an error. The object form simply returns false, and "wash car" is still in the list.

Frequently Asked Questions

Call remove() for the head, remove(index) for a position, or remove(object) for the first matching value.
The no-argument and index forms return the removed element. The object form returns true if something was removed and false otherwise.
An int argument selects the index form. To remove the value 2, pass an object: list.remove(Integer.valueOf(2)).
The no-argument form throws NoSuchElementException. Use poll() to get null instead.
No. It deletes only the first match. Use removeAll or removeIf to delete every match.

Next: removeAll()

Remove every element that also appears in another collection.

removeAll() 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