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
Call
What 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
Parameter
Description
o (Object)
The element to remove. Matched with equals. May be null.
Return Value
Return Value
Description
boolean
true if an element was removed, false if the list did not contain o.