LinkedList removeFirstOccurrence() Method in Java Last Updated : 20 Dec, 2024 Comments Improve Suggest changes Like Article Like Report In Java, the removeFirstOccurrence() method of the LinkedList class is used to remove the first occurrence of the specified element from the list. If there is no occurrence of the specified element the list remains unchanged.Example 1: Here, we use the removeFirstOccurence() method to remove the first occurrence of the specified element in a LinkedList of Strings. Java // Use of removeFirstOccurrence() with Integers import java.util.LinkedList; public class Geeks { public static void main(String[] args) { // Creating an Empty LinkedList LinkedList<String> l = new LinkedList<>(); // Use add() to add // elements in the list l.add("A"); l.add("B"); l.add("C"); l.add("B"); l.add("D"); // Displaying the original LinkedList System.out.println("" + l); // removeFirstOccurrence() is // going to return true System.out.println( "First Occurence of B is removed: " + l.removeFirstOccurrence("B")); // Here M is not present in the Linkedlist // so removeFirstOccurrence() is going to return // false System.out.println( "M is present in the Linked List: " + l.removeFirstOccurrence("M")); // Displaying the new LinkedList System.out.println("" + l); } } Output[A, B, C, B, D] First Occurence of B is removed: true M is present in the Linked List: false [A, C, B, D] Syntax of LinkedList removeFirstOccurrence() Methodpublic boolean removeFirstOccurrence(Object o)Return Type: This method is going to return true, if the element is found and removed from the list.This method is going to return false, if the element is not present in the list.Example 2: Here, we use the removeFirstOccurence() method to remove the first occurrence of an integer element from the LinkedList. Java // Use of removeFirstOccurrence() with Integers import java.util.*; public class Geeks { public static void main(String args[]) { // Creating an Empty LinkedList LinkedList<Integer> l = new LinkedList<>(); // Use add() to add // elements in the list l.add(10); l.add(20); l.add(30); l.add(20); l.add(40); // Displaying the original LinkedList System.out.println("" + l); // removeFirstOccurrence() is // going to return true System.out.println( "First Occurence of 20 is removed: " + l.removeFirstOccurrence(20)); // Here 100 is not present in the Linkedlist // so removeFirstOccurrence() is // false System.out.println( "100 is present in the Linked List: " + l.removeFirstOccurrence(100)); // Displaying the new LinkedList System.out.println("" + l); } } Output[10, 20, 30, 20, 40] First Occurence of 20 is removed: true 100 is present in the Linked List: false [10, 30, 20, 40] Comment More infoAdvertise with us Next Article LinkedList removeFirstOccurrence() Method in Java S ShivamKD Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions java-LinkedList +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads LinkedList descendingIterator() Method in Java In Java, the descendingIterator() method of LinkedList is used to return an iterator that allows to traverse the list in reverse order.Example 1: This example demonstrates how to use descendingIterator() method with Strings in a LinkedList.Java// Java program to use // descendingIterator() import ja 3 min read LinkedList element() Method in Java In Java, the element() method of the LinkedList class is used to retrieve the first element in the list without removing it. The first element of the LinkedList is known as the head. Example 1: Here, we use the element() method to retrieve the first element of the LinkedList of Integers, without rem 2 min read Java.util.LinkedList.get(), getFirst(), getLast() in Java In Java, the LinkedList class provides several methods for accessing the elements in the list. Here are the methods for getting the elements of a LinkedList: get(int index): This method returns the element at the specified position in the LinkedList. The index parameter is zero-based, so the first e 5 min read LinkedList getLast() Method in Java In Java, the getLast() method in the LinkedList class is used to retrieve the last element of the list.Example 1: Here, we use the getLast() method to retrieve the last element of the list.Java// Java Program to Demonstrate the // use of getLast() in LinkedList import java.util.LinkedList; public cl 2 min read LinkedList indexOf() Method in Java In Java, the indexOf() method of the LinkedList class is used to find the index of the first occurrence of the specified element in the list. Syntax of LinkedList indexOf() Method public int indexOf(Object o);Parameter: This method takes an object "o" as an argument.Return type: It returns the index 2 min read LinkedList lastIndexOf() Method in Java In Java, the lastIndexOf() method of LinkedList is used to find the last occurrence of the specified element in the list. If the element is present it will return the last occurrence of the element, otherwise, it will return -1.Syntax of LinkedList lastIndexOf() Method public int lastIndexOf(Object 2 min read LinkedList listIterator() Method in Java In Java, the listIterator() method of the LinkedList class returns a ListIterator that allows us to iterate over the elements of the list.Example: Java// Java Program to Demonstrate the // use of listIterator() in LinkedList import java.util.LinkedList; import java.util.ListIterator; public class Ge 3 min read Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java In Java, the LinkedList class is present in java.util package, and it also has different methods that do the work of flexible addition of elements and help addition both at the front and back of the list. In this article, we cover the LinkedList offer(), offerFirst(), and offerLast() methods. These 5 min read Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java Linked list class offers the functionality to "look into" the first and last elements of the list and hence can be useful in cases where only the retrieval is required and not necessarily the deletion is required. Three functionalities are present and all are discussed in this article. 1. peek() : T 4 min read Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java Java's Linked list class offers a function that allows a "Queue Based" working called poll(). This function not only returns deletes the first element, but also displays them while being deleted and hence can have a lot of usage in daily life problems and competitive programming as well. There are 3 4 min read Like