LinkedList pop() Method in Java Last Updated : 19 Dec, 2024 Comments Improve Suggest changes Like Article Like Report In Java, the pop() method of the LinkedList class is used to remove and return the top element from the stack represented by the LinkedList. The method simply pops out an element present at the top of the stack. This method is similar to the removeFirst method in LinkedList.Example 1: Here, we use the pop() method to remove and return the first element (head) of the LinkedList. Java // Java Program to Demonstrate the // use of pop() in LinkedList import java.util.LinkedList; 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(100); l.add(200); l.add(300); l.add(400); System.out.println("" + l.pop()); System.out.println("" + l); } } Output100 [200, 300, 400] Syntax of LinkedList pop() Methodpublic E pop()Return Type: This method returns the element that is removed from the head of the list.Exception: If the list is empty, calling pop() will throw a NoSuchElementException.Example 2: Here, the pop() is going to throw an NoSuchElementException if the list is empty. Java // Handling an empty LinkedList with pop() import java.util.LinkedList; class Geeks { public static void main(String[] args) { // Here we are trying to pop // an element from an empty list try { LinkedList<String> l = new LinkedList<>(); l.pop(); } catch (Exception e) { System.out.println("Exception caught: " + e); } } } OutputException caught: java.util.NoSuchElementException Comment More infoAdvertise with us Next Article LinkedList pop() 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 clear() Method in Java In Java, the clear() is used to remove all the elements from a LinkedList. This method only clears all the element from the list and not deletes the list. After calling this method, the list will be empty.Syntax of LinkedList clear() Methodvoid clear()Parameters: This method does not accept any para 1 min read LinkedList clone() Method in Java In Java, the clone() method of LinkedList, creates a shallow copy which means the structure is duplicated but the objects inside the list are shared between the original and the copied list. So, the cloned list will have the same elements as the original list.Syntax of Java LinkedList clone() Method 1 min read 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 Like