How to iterate through LinkedList in reverse order

In this article, we will discuss how to iterate through contents of LinkedList using descendingIterator() method

 

To iterate LinkedList contents in reverse-order :

Method signature:

public Iterator descendingIterator();
  • This method returns an iterator to iterate over elements in this deque in reverse sequential order
  • The elements will be returned in order from last (tail) to first (head)
  • Note: this is available from Java 1.6 version

 

Example 1: using descendingIterator() method of LinkedList class

IterateLinkedListContentInReverseOrder.java

package in.bench.resources.java.collections;

import java.util.Iterator;
import java.util.LinkedList;

public class IterateLinkedListContentInReverseOrder {

	public static void main(String[] args) {

		// creating LinkedList object of type String
		LinkedList<String> techCompanies = new LinkedList<String>();

		// adding elements to LinkedList object
		techCompanies.add("Sun");
		techCompanies.add("Apple");  
		techCompanies.add("JBoss"); 
		techCompanies.add("Whatsup");
		techCompanies.add("Android");
		techCompanies.add("BEA Weblogic");
		techCompanies.add("Apache");

		// Iterating using enhanced for-loop
		System.out.println("Insertion Order: \n");
		for(String techCompany : techCompanies) {
			System.out.println(techCompany);
		}

		// get descending Iterator to iterate in reverse order
		Iterator<String> dItr = techCompanies.descendingIterator();

		// Iterating using enhanced for-loop
		System.out.println("\n\n\nReverse of Insertion Order: \n");
		while(dItr.hasNext()){
			System.out.println(dItr.next());
		}
	}
}

Output:

Insertion Order: 

Sun
Apple
JBoss
Whatsup
Android
BEA Weblogic
Apache



Reverse of Insertion Order: 

Apache
BEA Weblogic
Android
Whatsup
JBoss
Apple
Sun

Note: there is other way to iterate LinkedList using reverse() method of Collections class

 

Example 2: using reverse() method of Collections class

ReverseIteratingLinkedList.java

package in.bench.resources.java.collections;

import java.util.Collections;
import java.util.LinkedList;

public class ReverseIteratingLinkedList {

	public static void main(String[] args) {

		// creating LinkedList object of type String
		LinkedList<String> techCompanies = new LinkedList<String>();

		// adding elements to LinkedList object
		techCompanies.add("Sun");
		techCompanies.add("Apple");  
		techCompanies.add("JBoss"); 
		techCompanies.add("Whatsup");
		techCompanies.add("Android");
		techCompanies.add("BEA Weblogic");
		techCompanies.add("Apache");

		// Iterating using enhanced for-loop
		System.out.println("Insertion Order: \n");
		for(String techCompany : techCompanies) {
			System.out.println(techCompany);
		}

		// reverse LinkedList contents using Collection.reverse(lst)
		Collections.reverse(techCompanies);

		// Iterating using enhanced for-loop
		System.out.println("\n\n\nAfter reversing using "
				+ "Collections.reverse(lst): \n");
		for(String techCompany : techCompanies) {
			System.out.println(techCompany);
		}
	}
}

Output:

Insertion Order: 

Sun
Apple
JBoss
Whatsup
Android
BEA Weblogic
Apache



After reversing using Collections.reverse(lst): 

Apache
BEA Weblogic
Android
Whatsup
JBoss
Apple
Sun

 

References:

 

Happy Coding !!
Happy Learning !!

LinkedList specific method examples
Replacing ArrayList element with new value using set() method