HomeLanguagesJavaIterate a LinkedList in Reverse Order in Java

Iterate a LinkedList in Reverse Order in Java

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
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32738 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7124 POSTS0 COMMENTS
Nicole Veronica
12254 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12337 POSTS0 COMMENTS
Shaida Kate Naidoo
7235 POSTS0 COMMENTS
Ted Musemwa
7497 POSTS0 COMMENTS
Thapelo Manthata
7214 POSTS0 COMMENTS
Umr Jansen
7184 POSTS0 COMMENTS