Java LinkedList lastIndexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
LinkedList

Definition

In Java, lastIndexOf(o) searches the LinkedList from the end toward the front and returns the index of the last element equal to o. If there is no match, it returns -1.

The returned index still counts from the front, as always. When a value appears only once, indexOf and lastIndexOf return the same number.

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

Syntax

Pass the value to look for.

Java
LinkedList<String> list = new LinkedList<>(Arrays.asList("A", "B", "C", "B"));
int a = list.lastIndexOf("B");   // 3, the last B
int b = list.indexOf("B");       // 1, the first B
int c = list.lastIndexOf("Z");   // -1, not found
CallWhat it does
list.lastIndexOf(o)Index of the last match, or -1.
list.indexOf(o)Index of the first match, or -1.
list.removeLastOccurrence(o)Removes the last match.
list.contains(o)Tells only whether a match exists.

Equality is tested with equals, and null is allowed.

Library declarations
Java
// java.util.LinkedList
public int lastIndexOf(Object o)

Parameters

ParameterDescription
o (Object)The element to look for. May be null.

Return Value

Return ValueDescription
intThe index of the last match, or -1 if the list does not contain o.

Compare with Similar Methods

Ways to search a list:

MethodHow it differs
indexOf(o)Finds the first match instead of the last.
removeLastOccurrence(o)Finds the last match and removes it.
contains(o)Returns just true or false.
ArrayList.lastIndexOf()The same method on an array-backed list.

Java Examples

Example 1 — Find the Last Position of a Value

Search for a value that repeats and compare the first and last positions.

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

public class LinkedListLastIndexOfDemo {
    public static void main(String[] args) {
        LinkedList<Integer> rolls = new LinkedList<>(Arrays.asList(6, 2, 6, 4, 6, 1));

        System.out.println("first 6: " + rolls.indexOf(6));
        System.out.println("last 6:  " + rolls.lastIndexOf(6));
        System.out.println("last 4:  " + rolls.lastIndexOf(4));
        System.out.println("last 9:  " + rolls.lastIndexOf(9));
    }
}

How It Works

  1. Three sixes. They are at indexes 0, 2 and 4.
  2. The two methods differ. indexOf reports 0 and lastIndexOf reports 4.
  3. A single match. The only 4 is at index 3, so the first and last positions would be the same.
  4. A missing value. There is no 9, so the answer is -1.

Example 2 — Measure the Gap Between the First and Last Copy

Use both search methods to see how much of the list lies between the first and last copy of a value.

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

public class LinkedListLastIndexOfGap {
    public static void main(String[] args) {
        LinkedList<String> log = new LinkedList<>(Arrays.asList(
            "start", "ok", "ERROR", "ok", "ok", "ERROR", "ok"));

        int first = log.indexOf("ERROR");
        int last = log.lastIndexOf("ERROR");

        System.out.println("first ERROR at " + first);
        System.out.println("last ERROR at " + last);
        System.out.println("entries between them: " + (last - first - 1));
        System.out.println("after the last one: " + (log.size() - 1 - last));
    }
}

How It Works

  1. Two errors. In seven log lines, "ERROR" appears at indexes 2 and 5.
  2. Both ends found. indexOf gives 2 and lastIndexOf gives 5.
  3. Entries in between. Subtracting the two indexes and then 1 gives 2, which are the two "ok" lines between the errors.
  4. Entries after the last one. The last valid index is 6, so one line comes after the final error.

Example 3 — Keep Everything Up to the Last Match

Find the last separator in a list of path parts and use it to split the list in two.

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

public class LinkedListLastIndexOfCut {
    public static void main(String[] args) {
        LinkedList<String> parts = new LinkedList<>(
            Arrays.asList("home", "/", "user", "/", "notes.txt"));

        int cut = parts.lastIndexOf("/");
        List<String> folder = new LinkedList<>(parts.subList(0, cut));
        List<String> file = new LinkedList<>(parts.subList(cut + 1, parts.size()));

        System.out.println("last slash at: " + cut);
        System.out.println("folder: " + folder);
        System.out.println("file: " + file);
    }
}

How It Works

  1. Path pieces. The list holds five parts, with a slash at indexes 1 and 3.
  2. lastIndexOf finds the final slash. The result is 3, not 1.
  3. Everything before it. subList(0, cut) copies home, "/" and user into the folder list.
  4. Everything after it. subList(cut + 1, size) gives only the file name, "notes.txt".

Frequently Asked Questions

The index of the last element equal to the given object, or -1 if the list does not contain it.
indexOf returns the first match, and lastIndexOf returns the last match. Both count the index from the front of the list.
When the value appears exactly once, or not at all (both return -1).
Yes. A LinkedList can walk backward from its tail, so a match near the end is found quickly.
Use removeLastOccurrence(o), which finds the last match and deletes it.

Next: listIterator()

Get an iterator that moves both ways and can add or replace elements.

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

14 people found this page helpful