Searching in linked list

In a recent post, we have seen how to add, remove, and retrieve the element from LinkedList in java. In this post, we will know how to search in linked list. We have some methods that help us to search an element in linked list. Let’s see how to perform Searching in linked list.

As we know Java LinkedList doesn’t support random access like array and ArrayList. When we perform searching in Linked List, it checks each element which means time complexity will be O(n). We can search in LinkedList by use of indexOf(), lastIndexOf(), contain() or manual way.  Let’s perform Searching in linked list

In this article

  • Using the indexOf(Object obj) method to find the first occurrence
  • Using the lastIndexOf(Object obj) method to find the last occurrence
  • Using the contains(Object o) method to check existence
  • Searching manually by traversing the LinkedList

1. indexOf(Object obj) method

The indexOf() method is used to find the index of the particular object. This method takes an argument and starts the search from the head and moves towards the tail. If an object finds, then it returns the index value else it returns -1. If  a LinkedList contains a duplicate object it returns the index value of the first occurrence.

public int indexOf(Object o)

accessModifier, this method is public, so it is accessible everywhere.
returnType, it returns int value.
o, the object which we want to search in LinkedList.

import java.util.LinkedList;
public class ExampleOfLinkedList
{  
    public static void main(String args[])
    {
    	LinkedList<String> data = new LinkedList<String>();
    	data.add("Java");
    	data.add("Goal");
    	data.add(".com");
    	data.add("Java");
    	data.add("Learning");
    	data.add("Website");
    	int index = data.indexOf("Java");
    	if(index != -1)
    	{
    		System.out.println("Element exists at index: "+ index);
    	}
    	else
    	{
    		System.out.println("Element not found");
    	}
    }  
}  

Output: Element exists at index: 0

AS you can see in above example. The indexOf() method returned the index 0, even the same object presented at index 3.  

2.  lastIndexOf(Obejct obj) method

The lastIndexOf(Object obj) method is also used to find the index value of the given object. This method also takes the parameter but it starts the search from the tail of LinkedList.

public int lastIndexOf(Object o)

accessModifier, this method is public, so it is accessible everywhere.
returnType, it returns int value.
o, the object which we want to search in LinkedList.

import java.util.LinkedList;
public class ExampleOfLinkedList
{  
    public static void main(String args[])
    {
    	LinkedList<String> data = new LinkedList<String>();
    	data.add("Java");
    	data.add("Goal");
    	data.add(".com");
    	data.add("Java");
    	data.add("Learning");
    	data.add("Website");
    	int index = data.lastIndexOf("Java");
    	if(index != -1)
    	{
    		System.out.println("Element exists at index: "+ index);
    	}
    	else
    	{
    		System.out.println("Element not found");
    	}
    }  
}  

Output: Element exists at index: 3

3. Contains(Object o) method

The contains(Object o) method is used to check whether the object o is present in LinkedList or not. It returns a boolean value. If an element exists, then it returns true otherwise false.

public boolean contains(Object o)

accessModifier, this method is public, so it is accessible everywhere.
returnType, it returns boolean value.
o, the object which we want to search in LinkedList.

import java.util.LinkedList;
public class ExampleOfLinkedList
{  
    public static void main(String args[])
    {
    	LinkedList<String> data = new LinkedList<String>();
    	data.add("Java");
    	data.add("Goal");
    	data.add(".com");
    	data.add("Java");
    	data.add("Learning");
    	data.add("Website");
    	boolean isExists = data.contains("Java");
    	if(isExists)
    	{
    		System.out.println("Element exists");
    	}
    	else
    	{
    		System.out.println("Element not found");
    	}
    }  
}  

Output: Element exists

By manual way

We can search the object by a manual way. We can traverse the LinkedList and match the element in LinkedList.

import java.util.LinkedList;
public class ExampleOfLinkedList
{  
    public static void main(String args[])
    {
    	LinkedList<String> data = new LinkedList<String>();
    	data.add("Java");
    	data.add("Goal");
    	data.add(".com");
    	data.add("Java");
    	data.add("Learning");
    	data.add("Website");
    	for(int i = 0; i < data.size(); i++)
    	{
    		if(data.get(i).equals("Java"))
    			System.out.println("Element exists index: "+ i);
    	}
    }
}  

Output: Element exists index: 0
Element exists index: 3

Frequently Asked Questions

What is the time complexity of searching in a LinkedList?

Because Java LinkedList does not support random access like an array or ArrayList, searching checks each element one by one, so the time complexity is O(n).

Which methods can be used to search an element in a LinkedList?

You can search a LinkedList using indexOf(), lastIndexOf(), contains(), or by manually traversing the list and comparing elements.

What does indexOf() return if the element is not found?

If the object is not present in the LinkedList, indexOf() returns -1.

What is the difference between indexOf() and lastIndexOf()?

indexOf() starts searching from the head and returns the index of the first occurrence, while lastIndexOf() starts from the tail and returns the index of the last occurrence.

What does contains() return when searching a LinkedList?

contains(Object o) returns a boolean value: true if the element exists in the LinkedList, otherwise false.

How do you find all occurrences of an element in a LinkedList?

You can traverse the LinkedList manually with a loop and compare each element, printing the index whenever a match is found.

Leave a Comment