Skip to content
Related Articles

Related Articles

Improve Article
Iterate a LinkedList in Reverse Order in Java
  • Last Updated : 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());
        }
    }
}
Output
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());
        }
    }
}
Output
2021
2020
Geek
For
Geeks

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :