How to get the first and the last element of 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("100");
		lList.add("200");
		lList.add("300");
		lList.add("400");
		lList.add("500");
		System.out.println("First element of LinkedList is :" + lList.getFirst());
		System.out.println("Last element of LinkedList is : " + lList.getLast());
	}
}

Output

First element of LinkedList is :100
Last element of LinkedList is :500