How do you find the element of a LinkedList in Java?



A Linked list is a linear type of data structure that is used to store a group of elements. It is a part of the Java Collections Framework.

A LinkedList is made up of nodes, where each node contains a data field and a reference to the next node in the sequence.

In this article, we will learn how to find an element in a LinkedList in Java.

Ways to Find an Element in a LinkedList

There are various ways to find an element in a LinkedList in Java. They are -

Using the contains() method

The contains() method of the LinkedList class accepts an object and checks if it exists in the current LinkedList. If found, it returns true; else, it returns false.

Syntax

LinkedList.contains(Object o)

Example

We will create a LinkedList of integer type and add some values to it. Then we will use the contains() method to check if a specific value exists in the LinkedList.

import java.util.LinkedList;
public class FindElementInLinkedList {
   public static void main(String[] args) {
      LinkedList<Integer> llist = new LinkedList<>();
      
      llist.add(10);
      llist.add(20);
      llist.add(30);
      llist.add(40);
      
      int elementToFind = 20;
      
      if (llist.contains(elementToFind)) {
         System.out.println("The element " + elementToFind + " is found in the LinkedList.");
      } else {
         System.out.println("The element " + elementToFind + " is not found in the LinkedList.");
      }
   }
}

Output

Following is the output of the above code:

The element 20 is found in the LinkedList.

Using the indexOf() method

The indexOf() method of the LinkedList class accepts an object and finds the (starting) index of it in the current LinkedList and returns it. Returns -1 if it doesn't find the given object in the current LinkedList.

LinkedList.indexOf(Object o)

Example

We will create a LinkedList of string type and add some values to it. Then we will use the indexOf() method to find the index of a specific value in the LinkedList.

import java.util.LinkedList;
public class FindElementInLinkedList {
   public static void main(String[] args) {
      LinkedList<String> llist = new LinkedList<>();
      
      llist.add("Apple");
      llist.add("Banana");
      llist.add("Cherry");
      llist.add("Date");
      
      String elementToFind = "Cherry";
      
      int index = llist.indexOf(elementToFind);
      if (index != -1) {
         System.out.println("The element " + elementToFind + " is found at index: " + index);
      } else {
         System.out.println("The element " + elementToFind + " is not found in the LinkedList.");
      }
   }
}

Output

Following is the output of the above code:

The element Cherry is found at index: 2

Using a while loop

We can also use a while loop to iterate through the LinkedList and find an element. If we use a while loop, we can perform additional operations while searching for the element.

Example

We will create a LinkedList of integer type and add some values to it. Then we will use a while loop to find a specific value in the LinkedList. We will run a while loop until we find the element or reach the end of the LinkedList.

import java.util.LinkedList;
public class FindElementInLinkedList {
   public static void main(String[] args) {
      LinkedList<Integer> llist = new LinkedList<>();
      
      llist.add(10);
      llist.add(20);
      llist.add(30);
      llist.add(40);
      
      int target = 30;
      boolean found = false;
      int index = 0;

      while (index < llist.size()) {
         if (llist.get(index).equals(target)) {
            found = true;
            break;
         }
         index++;
      }

      if(found) {
         System.out.println("The element " + target + " is found at index: " + index);
      } else {
         System.out.println("The element " + target + " is not found in the LinkedList.");
      }
   }
}

Output

Following is the output of the above code:

The element 30 is found at index: 2
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-06-05T13:32:16+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements