LinkedList removeLastOccurrence() method in Java with Example

The java.util.concurrent.LinkedList.removeLastOccurrence() method is an inbuilt method in Java which accepts a parameter and removes the last appearance of that element in the list. Thus, in case the element is not present in the list, it remains unchanged.

Syntax:

public boolean removeLastOccurrence(Object o)

Parameters: The function accepts an object elem as parameter which denotes the object whose last appearance from the list is to be removed.



Return Value: The function returns true if elem is present in th list and returns false otherwise.

Below programs illustrate the use of removeLastOccurrence() method :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate removeLastOccurence()
// method of LinkedList
  
import java.util.*;
  
class LinkedListDemo {
    public static void main(String[] args)
    {
        LinkedList<String> list
            = new LinkedList<String>();
  
        list.add("GFG");
        list.add("Geeks");
        list.add("Gfg");
        list.add("gfg");
        list.add("Geeks");
  
        // Displaying the existing LinkedList
        System.out.println("LinkedList: "
                           + list);
  
        // Remove last occurence of  element
        list.removeLastOccurrence("Geeks");
  
        System.out.println("Removed last occurrence"
                           + " of 'Geeks' from the list");
  
        // Displaying the modified LinkedList
        System.out.println("LinkedList: "
                           + list);
    }
}

chevron_right


Output:

LinkedList: [GFG, Geeks, Gfg, gfg, Geeks]
Removed last occurrence of 'Geeks' from the list
LinkedList: [GFG, Geeks, Gfg, gfg]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate removeLastOccurence()
// method of LinkedList
  
import java.util.*;
  
class LinkedListDemo {
    public static void main(String[] args)
    {
        LinkedList<Integer>
            list = new LinkedList<Integer>();
  
        list.add(12);
        list.add(280);
        list.add(12);
        list.add(1050);
        list.add(12);
  
        // Displaying the existing LinkedList
        System.out.println("LinkedList: "
                           + list);
  
        // Remove last occurence of  element
        list.removeLastOccurrence(12);
  
        System.out.println("Removed last occurrence"
                           + " of '12' from the list");
  
        // Displaying the modified LinkedList
        System.out.println("LinkedList: "
                           + list);
    }
}

chevron_right


Output:

LinkedList: [12, 280, 12, 1050, 12]
Removed last occurrence of '12' from the list
LinkedList: [12, 280, 12, 1050]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.