# Delete first and last element from a LinkedList in Java

Java 8Object Oriented ProgrammingProgramming

The first element can be deleted from a LinkedList by using the method java.util.LinkedList.removeFirst(). This method does not have any parameters and it returns the first element of the LinkedList.

The last element can be deleted from a LinkedList by using the method java.util.LinkedList.removeLast(). This method does not have any parameters and it returns the last element of the LinkedList.

A program that demonstrates this is given as follows −

## Example

Live Demo

import java.util.LinkedList;
public class Demo {
public static void main(String[] args) {
System.out.println("The LinkedList is: " + l);
l.removeFirst();
l.removeLast();
System.out.println("The LinkedList is: " + l);
}
}

## Output

The LinkedList is: [Apple, Mango, Pear, Orange, Guava]
The LinkedList is: [Mango, Pear, Orange]

Now let us understand the above program.

LinkedList<String> l = new LinkedList<String>();
System.out.println("The LinkedList is: " + l);
l.removeFirst();
System.out.println("The LinkedList is: " + l);