Iterate a LinkedList in Reverse Order in Java - GeeksforGeeks

28 Jan, 2021
For traversing a linked list in reverse order we can use Descending Iterator or List Iterator
1.  Descending Iterator
Syntax:
LinkedList<String> linkedlist = new LinkedList<>();

Iterator<String> listIterator = linkedlist.descendingIterator();
Returns: Descending Iterator returns the Iterator that points to the end of the linked list.
2.  List Iterator
Syntax: 
LinkedList<String> linkedlist = new LinkedList<>();

ListIterator<String> listIerator = linkedlist.listIterator(linkedlist.size());
Parameter: the size of the linked list, this will make the iterator point at the end of the linked list.
Example 1: Using descending Iterator
Java
// Java program to Iterate a LinkedList in Reverse Order// using descending Iterator  import java.util.Iterator;import java.util.LinkedList;  public class GFG {    public static void main(String[] args)    {        LinkedList<String> linkedList = new LinkedList<>();                // adding elements to linked list        linkedList.add("Geeks");        linkedList.add("For");        linkedList.add("Geek");        linkedList.add("2020");        linkedList.add("2021");          // getting an iterator which points at the        // end of the linkedlist        Iterator<String> iterator = linkedList.descendingIterator();          // traversing the linkedlist        // hasNext() will tell if previous element is        // available or not        // next() with descending iterator will return the        // previous element        // and after getting the previous element        // is moves the cursor to next previous element.        while (iterator.hasNext())         {            System.out.println(iterator.next());        }    }}
2021
2020
Geek
For
Geeks

Example 2: Using List Iterator
Java
// Java program to Iterate a LinkedList in Reverse Order// using List Iterator  import java.util.LinkedList;import java.util.ListIterator;  public class GFG {    public static void main(String[] args)    {          LinkedList<String> linkedList = new LinkedList<>();          // adding elements of to the linkedlist        linkedList.add("Geeks");        linkedList.add("For");        linkedList.add("Geek");        linkedList.add("2020");        linkedList.add("2021");          // getting an iterator that points at the end of the        // linkedlist        ListIterator<String> listIterator = linkedList.listIterator(linkedList.size());          // Traversing the linked list        // hasPrevious() function to check if previous        // element is present or not previous() function to        // get the previous element and after getting        // previous elements it move the cursor to the next        // previous element        while (listIterator.hasPrevious())        {            System.out.println(listIterator.previous());        }    }}
2021
2020
Geek
For
Geeks

Java-Collections
java-LinkedList
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?
Program to print ASCII Value of a character