Remove first and last elements of LinkedList Java example

 
import java.util.LinkedList;

public class Main {
  public static void main(String[] args) {
    LinkedList<String> lList = new LinkedList<String>();

    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");

    System.out.println(lList);
    Object object = lList.removeFirst();

    System.out.println(object + " has been removed");
    System.out.println(lList);

    object = lList.removeLast();
    System.out.println(object + " has been removed");
    System.out.println(lList);
  }
}
  

Output:


[1, 2, 3, 4, 5]
 1 has been removed
[2, 3, 4, 5]
 5 has been removed
[2, 3, 4]
Home 
  Java Book 
    Runnable examples  

Collection LinkedList:
  1. Add value to the first position in a linked list
  2. Convert a LinkedList to ArrayList
  3. Remove first and last elements of LinkedList Java example