LinkedList indexOf() Method in Java (original) (raw)

Last Updated : 11 Jul, 2025

In Java, the **indexOf() method of the LinkedList class is used to find the index of the first occurrence of the specified element in the list.

**Syntax of LinkedList indexOf() Method

public int indexOf(Object o);

Parameter: This method takes an object "o" as an argument.

**Return type:

**Example: Here, we use the indexOf() method **to find the index of the specified element in the list.

Java `

// Java Program to Demonstrate the // use of indexOf() in the LinkedList import java.util.LinkedList;

public class Geeks { public static void main(String args[]) {

    // Create an empty list
    LinkedList<Integer> l = new LinkedList<>();

    // Use add() method to add 
    // elements in the list
    l.add(10);
    l.add(20);
    l.add(30);
    l.add(40);
    l.add(50);

    System.out.println("The LinkedList is: " + l);

    // Use indexOf() to return 
    // the index of an element
    System.out.println(
        "The first occurrence of 20 is at index:"
        + l.indexOf(20));
    System.out.println(
        "The first occurrence of 90 is at index: "
        + l.indexOf(90));
}

}

`

Output

The LinkedList is: [10, 20, 30, 40, 50] The first occurrence of 20 is at index:1 The first occurrence of 90 is at index: -1

**Points to Remember: