LinkedList removeFirst() Method in Java Last Updated : 19 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, the removeFirst() method of the LinkedList class is used to remove and return the first element of the list.Example 1: Here, we use the removeFirst() method to remove the first element (head) of the LinkedList of Integers. Java // Java Program to demonstrate the // use of removeFirst() 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 First element // form the list System.out.println("Removed First element: " + l.removeFirst()); System.out.println("" + l); } } Output[100, 200, 300, 400] Removed First element: 100 [200, 300, 400] Syntax of LinkedList remove() Methodpublic E removeFirst()Return Type: The method returns the first element (head) that is removed from the list.Exception: If the list is empty, calling removeFirst() will throw a NoSuchElementException.Example 2: Here, the removeFirst() is going to throw an NoSuchElementException if the list is empty. Java // Handling NoSuchElementException with removeFirst() import java.util.LinkedList; class Geeks { public static void main(String[] args) { // Here we are trying to remove // first element from an empty list try { LinkedList<String> l = new LinkedList<>(); l.removeFirst(); } catch (Exception e) { System.out.println("Exception caught: " + e); } } } OutputException caught: java.util.NoSuchElementException Comment More infoAdvertise with us Next Article LinkedList descendingIterator() Method in Java C chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions java-LinkedList +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads LinkedList contains() Method in Java In Java, the contains() method of LinkedList is used to check whether an element is present in a LinkedList or not. It takes the element as a parameter and returns True if the element is present in the list.Syntax of Java LinkedList contains() Method boolean contains(Object element);Parameter: The p 2 min read 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 Like