In this article we will discuss how to iterate over a LinkedList in reverse order.
Method 1 : Using Descending Iterator
Iterator<E> descendingIterator()
Descending Iterator returns the Iterator that points to the end of of List i.e.
LinkedList<String> listOfStr = new LinkedList<>(); // Get Descending List Iterator from List Object Iterator<String> listItReverse = listOfStr.descendingIterator();
Now calling function next() with descending iterator will return the previous element and also moves the cursor by one position backward. Also, hasNext() will tell if previous element is available or not i.e.
// Iterate through LinkedList in Reverse Direction while(listItReverse.hasNext()) { String temp = listItReverse.next(); System.out.println(temp); }
Method 2 : Using Normal List Iterator
Get the list iterator that with index location List Size. This will make the iterator point to the end of List.
// Get ListIterator from List Object that // points to the end of List ListIterator<String> listIt = listOfStr.listIterator(listOfStr.size());
Now, use the ListIterator’s hasPrevious() function to check if previous element is present or not.
[showads ad=inside_post]
Then previous() function to fetch the previous element and move the cursor to previous element i.e.
Frequently Asked:
- Java LinkedList Tutorial
- How to iterate LinkedList in reverse or backward direction
- How to add elements in a LinkedList in Java
- How to add elements at a particular index in LinkedList
// Iterate through LinkedList in backward direction while(listIt.hasPrevious()) { String temp = listIt.previous(); System.out.println(temp); }
Checkout the complete example to iterate over a LinkedList using both the above two methods as follows,
package com.thispointer.java.collections.linkedlist; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; public class Example4 { public static void main(String[] args) { // Create a LinkedList of String objects LinkedList<String> listOfStr = new LinkedList<>(); // Add Elements at the end of LinkedList listOfStr.add("333"); listOfStr.add("456"); listOfStr.add("666"); listOfStr.add("789"); listOfStr.add("999"); // Display List in Reverse Order Method 1 // Get ListIterator from List Object Iterator<String> listItReverse = listOfStr.descendingIterator(); System.out.println("Iterate List in backword Direction Using Descending Iterator"); // Iterate through LinkedList in Reverse Direction while(listItReverse.hasNext()) { String temp = listItReverse.next(); System.out.println(temp); } // Display List in Reverse Order Method 2 // Get ListIterator from List Object that // points to the end of List ListIterator<String> listIt = listOfStr.listIterator(listOfStr.size()); System.out.println("Iterate List in backword Direction Using previous()"); // Iterate through LinkedList in backward direction while(listIt.hasPrevious()) { String temp = listIt.previous(); System.out.println(temp); } } }
Output:
Iterate List in backword Direction Using Descending Iterator 999 789 666 456 333 Iterate List in backword Direction Using previous() 999 789 666 456 333