In this example we will show the common method to remove first and last elements of LinkedList object in Java.
Source Code
package com.beginner.examples;
import java.util.LinkedList;
public class RemoveFirstLastElementsLinkedList {
public static void main(String[] args) {
//create LinkedList.
LinkedList linkedList = new LinkedList();
linkedList.add("A");
linkedList.add("B");
linkedList.add("C");
linkedList.add("D");
/*
* Use removeFirst() and removeLast() method tp remove first and last elements.
*/
linkedList.removeFirst();
linkedList.removeLast();
System.out.println(linkedList);
}
}
Output:
[B, C]
References
Imported packages in Java documentation: