Java LinkedList removeFirstOccurrence() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, removeFirstOccurrence(o) looks through the LinkedList from the head and removes the first element that is equal to o. It returns true if it removed something and false if there was no match.

Only one element is removed, even when the value appears several times. Elements are compared with equals, and null can be searched for too.

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 value to remove.

Java
LinkedList<String> list = new LinkedList<>();
list.add("A"); list.add("B"); list.add("A");
boolean r1 = list.removeFirstOccurrence("A");  // true, [B, A]
boolean r2 = list.removeFirstOccurrence("Z");  // false, no change
CallWhat it does
list.removeFirstOccurrence(o)Removes the first match, searching from the head.
list.removeLastOccurrence(o)Removes the last match, searching from the tail.
list.remove(o)The List name for exactly the same action.
list.removeAll(c)Removes every element found in a collection.

For a LinkedList, remove(Object) and removeFirstOccurrence(Object) do the very same thing.

Library declarations
Java
// java.util.LinkedList
public boolean removeFirstOccurrence(Object o)

Parameters

ParameterDescription
o (Object)The element to remove. Matched with equals. May be null.

Return Value

Return ValueDescription
booleantrue if an element was removed, false if the list did not contain o.

Compare with Similar Methods

Ways to remove by value:

MethodHow it differs
removeLastOccurrence(o)Same idea, but starts the search from the tail.
remove(o)The List name for the same action.
removeAll(c)Removes every element that appears in another collection.
ArrayDeque.removeFirstOccurrence()The array-based deque version.

Java Examples

Example 1 — Remove the First Copy of a Value

Remove one "red" from a list that contains it twice, and then remove a color that is not there.

Java
import java.util.LinkedList;

public class LinkedListRemoveFirstOccDemo {
    public static void main(String[] args) {
        LinkedList<String> colors = new LinkedList<>();
        colors.add("red");
        colors.add("green");
        colors.add("red");
        colors.add("blue");

        boolean first = colors.removeFirstOccurrence("red");
        System.out.println("removed red: " + first + " -> " + colors);

        boolean missing = colors.removeFirstOccurrence("pink");
        System.out.println("removed pink: " + missing + " -> " + colors);
    }
}

How It Works

  1. "red" appears twice. The list is red, green, red, blue.
  2. The search starts at the head. The first red, at index 0, is removed and the call returns true.
  3. The second red stays. The list becomes green, red, blue.
  4. No match, no change. "pink" is not in the list, so the call returns false.

Example 2 — Remove All Copies with a Loop

Call removeFirstOccurrence repeatedly until it returns false, and count the removals.

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

public class LinkedListRemoveFirstOccLoop {
    public static void main(String[] args) {
        LinkedList<Integer> nums = new LinkedList<>(Arrays.asList(4, 7, 4, 9, 4, 1));
        System.out.println("before: " + nums);

        int removed = 0;
        while (nums.removeFirstOccurrence(4)) {
            removed++;
        }

        System.out.println("removed " + removed + " copies");
        System.out.println("after: " + nums);
    }
}

How It Works

  1. Three 4s. The list is 4, 7, 4, 9, 4, 1.
  2. The loop test is the removal. The call both removes a 4 and reports whether it did, so the loop continues while it keeps returning true.
  3. It ends on false. After the third 4 is gone, the next call returns false and the loop stops.
  4. Counted and cleaned. Three copies were removed, and 7, 9 and 1 remain in their original order.

Example 3 — Remove null and a Number Value

Remove a null element, and remove a number as a value with no index confusion.

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

public class LinkedListRemoveFirstOccNull {
    public static void main(String[] args) {
        LinkedList<String> names = new LinkedList<>(Arrays.asList("Amy", null, "Bo", null));
        names.removeFirstOccurrence(null);
        System.out.println("one null removed: " + names);

        LinkedList<Integer> ids = new LinkedList<>(Arrays.asList(5, 2, 0, 3));
        boolean gone = ids.removeFirstOccurrence(2);
        System.out.println("value 2 removed: " + gone + " -> " + ids);
        System.out.println("0 still present: " + ids.contains(0));
    }
}

How It Works

  1. null is a legal search value. The list holds two nulls, and the first one is removed.
  2. The second null stays. The result is Amy, Bo, null.
  3. A number is a value here. removeFirstOccurrence(2) boxes the 2 and removes the value 2, not the element at index 2.
  4. The 0 is still there. If 2 had been used as an index, the element 0 at index 2 would have been removed instead, but contains(0) is true.

Frequently Asked Questions

It removes the first element equal to the given object, searching from the head, and returns true if it removed one.
There is none for a LinkedList. They do the same work. removeFirstOccurrence is the name used by the Deque interface.
No, only the first one. Use removeAll or removeIf to remove every match.
It returns false and leaves the list unchanged.
Yes. removeFirstOccurrence always takes an object, so removeFirstOccurrence(2) removes the value 2.

Next: removeIf()

Remove every element that passes a test, using a lambda.

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

12 people found this page helpful