Java LinkedList removeLastOccurrence() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, removeLastOccurrence(o) searches the LinkedList from the tail toward the head and removes the first match it meets, which is the last element equal to o in list order. It returns true if it removed something.

Use it when later copies matter more than earlier ones, for example to undo the most recent entry of a value. The comparison uses equals, and only one element is removed.

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 r = list.removeLastOccurrence("A");   // true, [A, B]
CallWhat it does
list.removeLastOccurrence(o)Removes the last match, searching from the tail.
list.removeFirstOccurrence(o)Removes the first match, searching from the head.
list.lastIndexOf(o)Finds the position of the last match without removing it.
list.removeIf(filter)Removes every element accepted by a test.

If the value appears only once, removeFirstOccurrence and removeLastOccurrence give the same result.

Library declarations
Java
// java.util.LinkedList
public boolean removeLastOccurrence(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
removeFirstOccurrence(o)Same idea, but starts the search from the head.
lastIndexOf(o)Reports where the last match is without removing it.
removeIf(filter)Removes every element that passes a test.
ArrayDeque.removeLastOccurrence()The array-based deque version.

Java Examples

Example 1 — Remove the Most Recent Copy of a Value

Remove the last "x" from a list that contains it three times.

Java
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

  1. Three x marks. They sit at indexes 0, 2 and 4.
  2. The search starts at the tail. The x at index 4 is met first, so that one is removed.
  3. The earlier copies stay. The list becomes x, o, x, o.
  4. A missing value gives false. There is no "z", so the last call returns false.

Example 2 — Compare the First and Last Occurrence

Remove one value from the head side in one list and from the tail side in another, from the same data.

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

public class LinkedListRemoveLastOccCompare {
    public static void main(String[] args) {
        LinkedList<Integer> a = new LinkedList<>(Arrays.asList(1, 2, 3, 2, 1));
        LinkedList<Integer> b = new LinkedList<>(a);

        a.removeFirstOccurrence(2);
        b.removeLastOccurrence(2);

        System.out.println("from the head: " + a);
        System.out.println("from the tail: " + b);
        System.out.println("same result: " + a.equals(b));
    }
}

How It Works

  1. Two copies of one list. The constructor copies 1, 2, 3, 2, 1 into list b.
  2. Head side. removeFirstOccurrence(2) removes the 2 at index 1.
  3. Tail side. removeLastOccurrence(2) removes the 2 at index 3.
  4. Different results. The lists are 1, 3, 2, 1 and 1, 2, 3, 1, so they are not equal.

Example 3 — Undo the Latest Entry of a Value

Keep a log of tags and undo only the most recent "bug" tag.

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

public class LinkedListRemoveLastOccUndo {
    public static void main(String[] args) {
        LinkedList<String> tags = new LinkedList<>(
            Arrays.asList("bug", "idea", "bug", "todo", "bug"));
        System.out.println("tags: " + tags);

        tags.removeLastOccurrence("bug");
        System.out.println("undo 1: " + tags);

        tags.removeLastOccurrence("bug");
        System.out.println("undo 2: " + tags);

        System.out.println("last bug at: " + tags.lastIndexOf("bug"));
    }
}

How It Works

  1. Three bug tags. They are at indexes 0, 2 and 4.
  2. First undo. The last bug tag, at index 4, is removed.
  3. Second undo. Now the last bug tag is the one at index 2, and it goes next.
  4. One left. Only the first bug tag is still there, and lastIndexOf finds it at index 0.

Frequently Asked Questions

It removes the last element equal to the given object, searching from the tail, and returns true if it removed one.
The first starts the search at the head, and the second starts at the tail. With duplicates they remove different elements.
No. It removes one element per call.
It returns false and leaves the list unchanged.
No. remove(Object) removes the first match. For the last match you need removeLastOccurrence, which LinkedList provides through the Deque interface.

Next: replaceAll()

Replace every element with the result of a function.

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

15 people found this page helpful