This example shows how to iterate LinkedList in reverse direction in Java. This example also shows how to iterate LinkedList in backward direction using ListIterator and descendingIterator method.
How to iterate LinkedList in reverse direction in Java?
There are a couple of ways using which you can iterate a LinkedList object in the reverse direction.
1. Using the ListIterator
A ListIterator in Java is an iterator that allows us to traverse the list in both directions. You can obtain a list iterator for the LinkedList object using the listIterator
method.
1 |
public ListIterator<E> listIterator(int index) |
The listIterator
method returns an object of the ListIterator starting from the specified index argument.
For iterating the linked list in reverse or backward direction, we need to pass the argument index as the LinkedList size. This way, the list iterator’s cursor will be positioned at the end of the list, and from there we will iterate the list backward towards the start.
Once you get the list iterator, iterate the LinkedList using the hasPrevious
and previous
methods as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.LinkedList; import java.util.ListIterator; public class LinkedListIterateReverseExample { public static void main(String[] args) { LinkedList<String> linkedListNumbers = new LinkedList<String>(); linkedListNumbers.add("One"); linkedListNumbers.add("Two"); linkedListNumbers.add("Three"); linkedListNumbers.add("Four"); linkedListNumbers.add("Five"); /* * First, get an object of the ListIterator * using the listIterator method. * * Position the cursor at the end of the * linked list by specifying the start index * as the list size */ ListIterator<String> listIterator = linkedListNumbers.listIterator( linkedListNumbers.size() ); /* * To iterate the linked list in reverse * direction, use the hasPrevious and previous * methods of the list iterator */ while(listIterator.hasPrevious()){ System.out.println( listIterator.previous() ); } } } |
Output
1 2 3 4 5 |
Five Four Three Two One |
2. Using the descendingIterator method
The descendingIterator
method of the LinkedList class returns an iterator over the linked list elements in reverse sequential order.
1 |
public Iterator<E> descendingIterator() |
The iterator object returned by this method will return elements in the reverse order, from last to first or tail to head. Once you get an iterator object, use the hasNext
and the next
methods of the Iterator in Java to iterate the linked list in the backward direction.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Iterator; import java.util.LinkedList; public class LinkedListIterateReverseExample { public static void main(String[] args) { LinkedList<String> linkedListNumbers = new LinkedList<String>(); linkedListNumbers.add("One"); linkedListNumbers.add("Two"); linkedListNumbers.add("Three"); linkedListNumbers.add("Four"); linkedListNumbers.add("Five"); /* * Get an descending iterator over linked list elements * using the descendingIterator method */ Iterator<String> iterator = linkedListNumbers.descendingIterator(); //iterate the linked list in backward direction while(iterator.hasNext()){ System.out.println( iterator.next() ); } } } |
Output
1 2 3 4 5 |
Five Four Three Two One |
This example is a part of the LinkedList in Java tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedList