Java LinkedList removeLastOccurrence() Method with Examples

In the previous article, we have discussed about Java LinkedList removeFirst() Method with Examples

In this article we are going to see the use of Java LinkedList removeLastOccurrence() method along with suitable examples.

Java LinkedList removeLastOccurrence() Method with Examples

This java.util.LinkedList.removeLastOccurrence() method is used to remove the last occurrence of a specified element from the LinkedList which means if same element is present at multiple positions of the list then from the last position the respective element gets removed. It returns true if that specific element is present and got removed else the list remains unchanged.

Syntax:

LinkedListName.removeLastOccurrence(Object o)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • Object o refers to the specified element from the LinkedList which is to be removed from last occurrence.

Let’s see removeLastOccurrence() method with suitable examples.

Example-1:Java LinkedList removeLastOccurrence() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add String elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then remove the last occurrence of the specific element from the LinkedList using removeLastOccurrence(Object o) method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("ram");
        l.add("shyam");
        l.add("gyan");
        l.add("ram");
        l.add("gyan");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // removing the last occurrence of 'gyan' from the LinkedList
        l.removeLastOccurrence("gyan");
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [ram, shyam, gyan, ram, gyan]
The new elements of LinkedList are: [ram, shyam, gyan, ram]

Example-2: Java LinkedList removeLastOccurrence() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add Integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then remove the last occurrence of the specific element from the LinkedList using removeLastOccurrence(Object o) method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(4);
        l.add(6);
        l.add(4);
        l.add(2);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // removing the last occurrence of 4 from the LinkedList
        l.removeLastOccurrence(4);
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [2, 4, 6, 4, 2]
The new elements of LinkedList are: [2, 4, 6, 2]

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: