Iterate LinkedList In Reverse Order | Java Programs

Output:

*** Iterate LinkedList In Reverse Order ***

Original linked list: [1998, 2000, 2010, 2012, 2018]
Linked List in Reverse Order:
2018
2012
2010
2000
1998

Click Here For Java Online Compiler

Solution:

/**
 *
 * @author Alee Ahmed Kolachi
 */
import java.util.LinkedList;
import java.util.Iterator;

public class IterateLinkedListInReverse {

    public static void main(String[] args) {

        LinkedList<String> linkedList = new LinkedList<>();
        System.out.println("*** Iterate LinkedList In Reverse Order ***" + "\n");
        linkedList.add("1998");
        linkedList.add("2000");
        linkedList.add("2010");
        linkedList.add("2012");
        linkedList.add("2018");

        System.out.println("Original linked list: " + linkedList);

        Iterator it = linkedList.descendingIterator();

        System.out.println("Linked List in Reverse Order:");
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon