Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaLinkedList removeLast() Method in Java

LinkedList removeLast() Method in Java

In Java, the LinkedList class provides the removeLast() method to remove and return the last element of the list. If the list is empty, the method throws a NoSuchElementException.

Example:

Java




import java.util.LinkedList;
 
public class Example {
    public static void main(String[] args)
    {
        LinkedList<String> list = new LinkedList<>();
 
        // add elements to the list
        list.add("apple");
        list.add("banana");
        list.add("orange");
        list.add("grape");
        System.out.println("Original list:");
        System.out.println(list);
 
        // remove the last element
        String removed = list.removeLast();
        System.out.println("Element removed: " + removed);
        System.out.println(list);
    }
}


Output

Original list:
[apple, banana, orange, grape]
Element removed: grape
[apple, banana, orange]

In this example, we first create a LinkedList object and add four elements to it. The System.out.println() statement prints the original list to the console.

We then use the removeLast() method to remove the last element from the list (which is “grape”) and print the removed element and the updated list to the console.

The Java.util.LinkedList.removeLast() method is used to remove the last element from the LinkedList. This method also returns the element after removing it.

Syntax: 

LinkedList.removeLast()

Parameters: This function does not take any parameters.

Return Value: The method returns the last element or the element present at the tail of the list.

The below program illustrates the Java.util.LinkedList.removeLast() method:

Java




// Java code to illustrate removeLast() method
 
import java.io.*;
import java.util.LinkedList;
 
public class LinkedListDemo {
 
    public static void main(String args[])
    {
        // Creating an empty LinkedList
        LinkedList<String> list = new LinkedList<String>();
 
        // Use add() method to add elements in the list
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
        list.add("10");
        list.add("20");
 
        // Displaying the list
        System.out.println("LinkedList:" + list);
 
        // Remove the tail using removeLast()
        System.out.println("The last element is removed: "
                           + list.removeLast());
 
        // Displaying the final list
        System.out.println("Final LinkedList:" + list);
 
        // Remove the tail using removeLast()
        System.out.println("The last element is removed: "
                           + list.removeLast());
 
        // Displaying the final list
        System.out.println("Final LinkedList:" + list);
    }
}


Output

LinkedList:[Geeks, for, Geeks, 10, 20]
The last element is removed: 20
Final LinkedList:[Geeks, for, Geeks, 10]
The last element is removed: 10
Final LinkedList:[Geeks, for, Geeks]

Time complexity: O(1)
Auxiliary Space: O(1)

RELATED ARTICLES

Most Popular

Recent Comments