Remove first and last elements of LinkedList - Java Collection Framework

Java examples for Collection Framework:LinkedList

Description

Remove first and last elements of LinkedList

Demo Code


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");
   /*w  ww  . j  a v a 2  s  . c  o  m*/
    System.out.println("LinkedList contains : " + lList);
   
    Object object = lList.removeFirst();
    System.out.println(object + " has been removed from the first index of LinkedList");
    System.out.println("LinkedList now contains : " + lList);
   
    object = lList.removeLast();
    System.out.println(object + " has been removed from the last index of LinkedList");
    System.out.println("LinkedList now contains : " + lList);
 
  }
}
 

Result


Related Tutorials