Example 1 — Remove the Most Recent Copy of a Value
Remove the last "x" from a list that contains it three times.
import java.util.LinkedList;
public class LinkedListRemoveLastOccDemo {
public static void main(String[] args) {
LinkedList<String> marks = new LinkedList<>();
marks.add("x");
marks.add("o");
marks.add("x");
marks.add("o");
marks.add("x");
boolean done = marks.removeLastOccurrence("x");
System.out.println("removed: " + done);
System.out.println("marks: " + marks);
System.out.println("missing: " + marks.removeLastOccurrence("z"));
}
} How It Works
- Three x marks. They sit at indexes 0, 2 and 4.
- The search starts at the tail. The x at index 4 is met first, so that one is removed.
- The earlier copies stay. The list becomes x, o, x, o.
- A missing value gives false. There is no "z", so the last call returns false.

