LinkedList listIterator() Method in Java Last Updated : 18 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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 Geeks { public static void main(String args[]) { // Create an empty list LinkedList<String> l = new LinkedList<>(); // Use add() to add // elements in the list l.add("Geeks"); l.add("For"); l.add("Geeks"); // Creating a ListIterator ListIterator<String> it = l.listIterator(); // Using ListIterator to traverse the list // hasNext() is used to check // if there are more elements in the list while (it.hasNext()) { // next() is used to retrieve // elements in the list System.out.print(it.next() + " "); } } } OutputGeeks For Geeks Now there are two versions of listIterator() method i.e. without index and with an index.1. listIterator() Method with No ParametersIt creates an ListIterator that starts form the beginning of the list.Syntax:public ListIterator<E> listIterator()Return type: It returns a ListIterator that starts from the beginning of the list.Example: Here, we use listIterator() to iterate over the elements of the list. Java // Iterate over the elements of the list import java.util.LinkedList; import java.util.ListIterator; public class Geeks { public static void main(String args[]) { // Create an empty list LinkedList<String> l = new LinkedList<>(); // Use add() to add // elements in the list l.add("A"); l.add("B"); l.add("C"); l.add("D"); l.add("E"); // Creating a ListIterator ListIterator<String> it = l.listIterator(); // Using ListIterator to traverse the list // hasNext() is used to check // if there are more elements in the list System.out.println("Traversing the List from index 0: "); while (it.hasNext()) { // next() is used to retrieve // elements in the list System.out.print(it.next() + " "); } } } OutputTraversing the List from index 0: A B C D E 2. listIterator() Method with an IndexThe listIterator(int index) method creates a ListIterator that starts iterating from the specified index in the list.Syntax:ListIterator<E> listIterator(int index);Parameter: index: The index form which the iterator will start iterating.Return type: It returns an iterator that starts from the specified index.Example: Here, we use the listIterator() method to iterate over the elements of the list from the specified index. Java // Iterate over the elements of the list // from the specified index import java.util.LinkedList; import java.util.ListIterator; class Geeks { public static void main(String[] args) { // Create 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); l.add(500); l.add(600); l.add(700); // Create an ListIterator ListIterator<Integer> it = l.listIterator(3); // Use the ListIterator to // traverse the list System.out.println( "Traversing the List forward from index 3: "); while (it.hasNext()) { System.out.print(it.next() + " "); } } } OutputTraversing the List forward from index 3: 400 500 600 700 Comment More infoAdvertise with us Next Article LinkedList listIterator() Method in Java 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 get() Method in Java In Java, the get() method of LinkedList is used to fetch or retrieve an element at a specific index from a LinkedList.Example 1: Here, we use the get() method to retrieve an element at a specified index.Java// Java Program to illustrate get() method import java.util.LinkedList; public class Geeks { 2 min read LinkedList addAll() Method in Java In Java, the addAll() method of the LinkedList class is used to add all the elements of one collection to another. This method takes a Collection as an argument and adds all its elements to the end of the list. Example: Here, we use the addAll() method to add all the elements of one collection to an 3 min read LinkedList addFirst() Method in Java In Java, the addFirst() method of LinkedList, adds elements at the beginning of the list. All the existing elements moved one position to the right and the new element is placed at the beginning.Syntax of LinkedList addFirst() Method public void addFirst( E e)Parameters: E is the data type of elemen 1 min read LinkedList addLast() Method in Java In Java, the addLast() method of the LinkedList class is used to add an element at the end of the list.Syntax of LinkedList addLast() Method void addLast( E e)Parameter: e is the element you want to add at the end of the list.Return type: This method does not return any value.Example: Here, we use t 1 min read 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 Like