How to search an element inside a linked list ?


Levels of difficulty: / perform operation:

JAVA Program

import java.util.LinkedList;
public class Main {
	public static void main(String[] args) {
		LinkedList lList = new LinkedList();
		lList.add("1");
		lList.add("2");
		lList.add("3");
		lList.add("4");
		lList.add("5");
		lList.add("2");
		System.out.println("First index of 2 is:"+lList.indexOf("2"));
		System.out.println("Last index of 2 is:"+ lList.lastIndexOf("2"));
	}
}

Output

First index of 2 is: 1
Last index of 2 is: 5