Try this:

// Substitute appropriate type.
ArrayList<...> a = new ArrayList<...>();

// Add elements to list.

// Generate an iterator. Start just after the last element.
ListIterator li = a.listIterator(a.size());

// Iterate in reverse.
while(li.hasPrevious()) {
  System.out.println(li.previous());
}
Answer from John Feminella on Stack Overflow
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ reverse-list-java
8 Ways to Reverse a List in Java | FavTutor
January 22, 2022 - Use a for loop to iterate over the list using two different counter variables: the first variable points to the beginning of the list and the second variable points to the end of the list.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 449084 โ€บ java โ€บ enhanced-loop-iterate-reverse-order
Using enhanced for loop to iterate in reverse order ? (Java in General forum at Coderanch)
Or you could use Collections.reverse() to reverse the order of the existing list. But offhand, it's probably easier to just use the old-fashioned for loop. Enhanced for isn't designed to handle everything - it just handles the most common case, and handles it very well.
Discussions

collections - Iterating through a list in reverse order in java - Stack Overflow
I'm migrating a piece of code to make use of generics. One argument for doing so is that the for loop is much cleaner than keeping track of indexes, or using an explicit iterator. In about half the More on stackoverflow.com
๐ŸŒ stackoverflow.com
[Java] Need help reversing integers of a List into a separate ArrayList.
The issue is that on line 18 you call reverse(null), which sets list to null in reverse so when you try to access anything in list it will give you a NullPointerException. More on reddit.com
๐ŸŒ r/learnprogramming
27
4
December 9, 2015
Can one do a for each loop in java in reverse order? - Stack Overflow
I need to run through a List in reverse order using Java. ... For clarity: I know how to iterate a list in reverse order but would like to know (for curiosity's sake ) how to do it in the for each style. ... The point of the "for-each" loop is that you just need to perform an operation on each ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
[JAVA] Is there a way to make this reverse method more efficient? Possibly requiring only one loop?
Why are you iterating from 0 to size and then have your calculation? You could directly iterate in the opposite direction Since you have index-based access to the list elements, you should be able to do an in-place swap via a third, temp element (single element, not a list). In this case, you only need a single loop and only need to iterate over half the list. More on reddit.com
๐ŸŒ r/learnprogramming
9
5
October 10, 2022
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java list โ€บ iterating backward through a list
Iterating Backward Through a List | Baeldung
June 27, 2025 - It was introduced in Java 1.2 as a replacement of Enumeration. The most simple implementation is to use a for loop to start from the last element of the list, and decrement the index as we reach the beginning of the list:
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java-program-to-reverse-a-list
How to reverse an array list in Java
June 14, 2024 - public class HelloWorld { public static void main(String[] args) { int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; System.out.println("Array before reverse:"); for (int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + " "); } for (int i = 0; i < numbers.length / 2; i++) { int temp = numbers[i]; numbers[i] = numbers[numbers.length - 1 - i]; numbers[numbers.length - 1 - i] = temp; } System.out.println("\nArray after reverse:"); for (int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + " "); } } }
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ can-one-do-a-for-each-loop-in-java-in-reverse-order
Java For-Each Loop: Can You Iterate a List in Reverse Order? โ€” javathinking.com
Prefer List.reversed() (Java 21+): Use this for clean, efficient reverse iteration with for-each. Use ListIterator for Compatibility: If stuck on Java <21, ListIterator is the most efficient general-purpose solution. Avoid Index-Based Loops for LinkedList: get(index) is slow for LinkedList; use ListIterator instead.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [java] need help reversing integers of a list into a separate arraylist.
r/learnprogramming on Reddit: [Java] Need help reversing integers of a List into a separate ArrayList.
December 9, 2015 -

This is a review question for my upcoming intro to programming exam and I need some help fixing my code. The question explicitly states to write a method that takes a List of integers and returns a new ArrayList in reverse order. It also says that we can assume we have methods prepend and append which add a new element to the beginning/end.

Here is my current code which gives me a NullPointerException

public static ArrayList<Integer> reverse(List<Integer> list){
	ArrayList<Integer> list2 = new ArrayList<Integer>();

	list.add(5);
	list.add(10);
	list.add(15);
	list.add(20);
	
	System.out.println(list);

	for(int i = 0, j = list.size()-1; i < j; i++){
		list2.add(i, list.remove(j));
	}
	
	return list2;
}
public static void main(String[] args){
	reverse(null);
}

Any help would be greatly appreciated. Thank you.

Edit: I am getting closer, 2 of the 4 objects are being reversed and displayed. More information is in the comments below.

Edit2: Thank you so much for all the help today guys, I understand this concept a lot better after reading through everyone's comments!

Find elsewhere
Top answer
1 of 16
165

One approach would be to reverse the whole list, but this would have O(n) performance with respect to its size. Note that the commonly-used Collections.reverse method actually reverses the original list in place, which may be an undesirable side-effect.

As a more efficient solution, you could write a decorator that presents a reversed view of a List as an Iterable. The iterator returned by your decorator would use the ListIterator of the decorated list to walk over the elements in reverse order.

For example:

Copypublic class Reversed<T> implements Iterable<T> {
    private final List<T> original;

    public Reversed(List<T> original) {
        this.original = original;
    }

    public Iterator<T> iterator() {
        final ListIterator<T> i = original.listIterator(original.size());
        
        return new Iterator<T>() {
            public boolean hasNext() { return i.hasPrevious(); }
            public T next() { return i.previous(); }
            public void remove() { i.remove(); }
        };
    }

    public static <T> Reversed<T> reversed(List<T> original) {
        return new Reversed<T>(original);
    }
}

And you would use it like:

Copyimport static Reversed.reversed;

...

List<String> someStrings = getSomeStrings();
for (String s : reversed(someStrings)) {
    doSomethingWith(s);
}

Update for Java 21

As per this answer, Java 21 includes an efficient List.reversed() method which does exactly what is requested.

2 of 16
103

For a list, you could use the Google Guava Library:

Copyfor (String item : Lists.reverse(stringList))
{
    // ...
}

Note that Lists.reverse doesn't reverse the whole collection, or do anything like it - it just allows iteration and random access, in the reverse order. This is more efficient than reversing the collection first.

To reverse an arbitrary iterable, you'd have to read it all and then "replay" it backwards.

(If you're not already using it, I'd thoroughly recommend you have a look at the Guava. It's great stuff.)

๐ŸŒ
HackerEarth
hackerearth.com โ€บ practice โ€บ notes โ€บ java-iterate-over-a-list-in-the-reverse-order-example
Java: Iterate over a list in the reverse order example - Yogendra Gadilkar
The ListIterator interface is a special kind of iterator designed to iterate over lists. It provides functionality that is not available in regular iterators such as iterating a list in either direction. With this in mind we can iterate a list in the reverse order by just using the ListIterator interface:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ iterate-list-in-reverse-order-in-java
Iterate List in Reverse Order in Java - GeeksforGeeks
July 23, 2025 - // Java program to iterate List in Reverse Order import java.util.*; class GFG { public static void main(String[] args) { // For ArrayList List<String> list = new ArrayList<String>(); // Add elements to list list.add("GEEKS"); list.add("for"); list.add("geeks"); // Generate an iterator to iterate List in reverse // order ListIterator<String> gfg_itr = list.listIterator(list.size()); // hasPrevious() returns true if the list has // previous element while (gfg_itr.hasPrevious()) { // Iterate in reverse System.out.println(gfg_itr.previous()); } // print list in Reverse using for loop for (int i =
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [java] is there a way to make this reverse method more efficient? possibly requiring only one loop?
r/learnprogramming on Reddit: [JAVA] Is there a way to make this reverse method more efficient? Possibly requiring only one loop?
October 10, 2022 -
//code inside a reverse method that has the list object 
//passed as a parameter from main method
int size = list.size();
//create temporary list object to hold contents of original list in reverse
MyListReferenceBased temp = new MyListReferenceBased(); 
for (int i = 0; i < size; i++) 
{ 
//list add method requires which index your adding to, and object
//get method just gets the object at the specified index in list
temp.add(i, list.get((size-i)-1)); 
//remove method just removes object at specified index
list.remove((size-i)-1);                       
} 
//now that original list is empty, put the contents back in 
for (int i = 0; i<size; i++)
{ 
list.add(i, temp.get(i));            
} 
temp.removeAll(); 
//end result: original list is now reversed and temporary array is all empty 
System.out.println("List has been Reversed."); 
//is there a way to get rid of the 2nd loop?? Not possible to just assign?

So this code is in a reverse method inside the main class for reversing a linked list. Well reversing a list in general, since the user of the main class isn't suppose to interact with the linked list directly but just has access to the linked list methods (Didn't do reverse inside linked list class on purpose). Rather in the main/driver class, I'm reversing the list by first creating a second/temporary linked list, adding over each of the elements from the original list (in reverse) into the new list. And then since I need the contents to be in the original list and only need the temporary list during the duration of this method, I copy over the elements back into the old list.

The list object is instantiated as local in the main method of this main class and then calls the reverse method, passing the list object as a parameter. Instead of having the second loop, isn't there a way to just assign the temporary list object to the original list? I was able to do this when I the underlying implementation of the list was using arrays, but not sure how to it with linked lists.

Or any other efficient work around? Remember this is specifically for reversing a linked list without directly being inside the linked list class.

๐ŸŒ
AlgoCademy
algocademy.com โ€บ link
Looping In Reverse in Java | AlgoCademy
In this lesson, we covered how to use a for loop to count backwards in Java. We discussed the basic concepts, provided examples, and highlighted common pitfalls and best practices. Mastering reverse loops is essential for writing efficient and readable code.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-traverse-through-arraylist-in-reverse-direction
Java Program to Traverse Through ArrayList in Reverse Direction - GeeksforGeeks
July 23, 2025 - Method 3: (Using For Loop) We know that List is an ordered collection, and we can access the element of the list just by its index, so Define an ArrayList and iterate from last using a for loop till the first element and print each element.
๐ŸŒ
w3resource
w3resource.com โ€บ java-exercises โ€บ collection โ€บ java-collection-exercise-11.php
Java - Reverse elements in an array list
May 21, 2025 - List before reversing : [Red, Green, Orange, White, Black] List after reversing : [Black, White, Orange, Green, Red] ... Write a Java program to reverse an ArrayList using Collections.reverse() and then print the reversed list.
๐ŸŒ
Codemia
codemia.io โ€บ knowledge-hub โ€บ path โ€บ iterating_through_a_list_in_reverse_order_in_java
Iterating through a list in reverse order in java
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ how-to-iterate-list-in-reverse-order-for-each-java
How to Iterate a List in Reverse Order Using a For-Each Loop in Java? - CodingTechRoom
Mistake: Trying to use for-each directly to iterate in reverse, leading to confusion. Solution: Instead, use a traditional for loop with an index for backward iteration.
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ how to reverse an array in java: 3 methods with examples
How to Reverse An Array In Java: 3 Methods With Examples
March 24, 2020 - Answer: There are three methods to reverse an array in Java. Using a for loop to traverse the array and copy the elements in another array in reverse order. Using in-place reversal in which the elements are swapped to place them in reverse order.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-reverse-a-list
Java Program to Reverse a List - GeeksforGeeks
July 23, 2025 - Input: "PLATFORM", "LEARNING", "BEST", "THE", "IS", "GFG" Output: Reverse order of given List :- [GFG, IS, THE, BEST, LEARNING, PLATFORM] ... The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. ... // Java Program to Reverse a List recursively import java.io.*; import java.util.*; class GFG { public static <T> void revlist(List<T> list) { // base condition when the list size is 0 if (list.size() <= 1 || list == null) return; T value = list.remove(0); // call the recursive function to reverse //
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java list โ€บ reverse an arraylist in java
Reverse an ArrayList in Java | Baeldung
April 3, 2025 - The Java standard library has provided the Collections.reverse method to reverse the order of the elements in the given List.