LinkedList removeLast() Method in Java (original) (raw)

Last Updated : 11 Jul, 2025

In Java, the **removeLast() method of LinkedList class is used to remove and return the last element of the list.

**Example 1: Here, we use the **removeLast() method **to remove and return the last element (tail) of the LinkedList.

Java `

// java Program to demonstrate the // use of removeLast() in LinkedList import java.util.LinkedList;

class Geeks { public static void main(String[] args) {

    // Creating an empty list
    LinkedList<Integer> l = new LinkedList<>();

    // use add() to add
    // elements in the list
    l.add(100);
    l.add(200);
    l.add(300);
    l.add(400);

    System.out.println("" + l);
  
        // Removing the Last element 
        // form the list
        System.out.println("Remove first element from the list: "
            + l.removeLast());

    System.out.println("" + l);
}

}

`

Output

[100, 200, 300, 400] Remove first element from the list: 400 [100, 200, 300]

**Syntax of LinkedList removeLast() Method

public E removeLast()

**Example 2: Here, the removeLast() is going to throw an NoSuchElementException if the list is empty.

Java `

// Handling NoSuchElementException with removeLast() import java.util.LinkedList; class Geeks { public static void main(String[] args){

    // Here we are trying to remove Last 
    // element from an empty list
    try {
        LinkedList<String> l = new LinkedList<>();
        
        l.removeLast();
    }
    catch (Exception e) {
        System.out.println("Exception caught: " + e);
    }
}

}

`

Output

Exception caught: java.util.NoSuchElementException