beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java – Remove first and last element from LinkedList example

By Chaitanya Singh | Filed Under: Java Collections

In this tutorial we will learn how to remove First and Last elements from LinkedList. In the few last posts we shared following tutorials:

1) Removing elements from a specific index
2) Removing specific element from LinkedList

Example

We have used removeFirst() method to remove first and removeLast() method to remove last element from LinkedList. Method definition and description are as follows:

1) public E removeFirst(): Removes and returns the first element from this list.
2) public E removeLast(): Removes and returns the last element from this list.

Complete Code:

import java.util.LinkedList;
public class RemoveExample {
 
 public static void main(String[] args) {
 
    // Create a LinkedList
    LinkedList<String> linkedlist = new LinkedList<String>();
 
    // Add elements to LinkedList
    linkedlist.add("Item1");
    linkedlist.add("Item2");
    linkedlist.add("Item3");
    linkedlist.add("Item4");
    linkedlist.add("Item5");
 
    // Displaying Elements before remove
    System.out.println("LinkedList Elements are:");
    for(String str: linkedlist){
       System.out.println(str);
    }
 
    // Removing First element
    Object firstElement = linkedlist.removeFirst();
    System.out.println("\nElement removed: "+ firstElement);
 
    // Removing last Element
    Object lastElement = linkedlist.removeLast();
    System.out.println("Element removed: "+ lastElement);
 
    // LinkedList elements after remove
    System.out.println("\nList Elements after Remove:");
    for(String str2: linkedlist){
       System.out.println(str2);
    }
 }
}

Output:

LinkedList Elements are:
Item1
Item2
Item3
Item4
Item5

Element removed: Item1
Element removed: Item5

List Elements after Remove:
Item2
Item3
Item4

Enjoyed this post? Try these related posts

  1. Java ArrayList addAll(Collection c) Method example
  2. How to iterate TreeMap in reverse order in Java
  3. Java ArrayList of Object Sort Example (Comparable And Comparator)
  4. Java – Remove all mappings from HashMap example
  5. Java ArrayList trimToSize() Method example
  6. How to find length of ArrayList in Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2019 BeginnersBook . Privacy Policy . Sitemap