Reverse a List in Java
The list interface is a member of Java Collections Framework.
In order to reverse a list, the first element of the list must be swapped with the last element, the second element with the second last element, and so on until we reach the middle element. Thus, the resultant list will be reversed list.
We can reverse a list in four different ways.
- Using add and remove method
- Using recursion
- Using reverse method of Collections
- Using ListIterator Interface
Different ways to reverse a list in Java
1. Reverse a list using add and remove method - Iterative approach
In this approach, we will use add and remove method of List interface. The add() method inserts the specified element at the specified position in this list. The remove() method deletes the element at the specified position in this list.
The syntax of add() and remove method used is as shown below. Here, for both the methods, index specifies the position of the element in the list. Whereas in add method, element specifies actual value to be added.
listobject.add(index,element);
listobject.remove(index);
Example : In this example, we will take a list of cities. Thereafter, we will use add, remove and, size method of List interface to reverse the list.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// Program to in-place reverse a list in Java
class Main {
public static void main(String[] args) {
List city = new ArrayList & lt; & gt;
(Arrays.asList("Mumbai", "Hyderabad", "Delhi", "Indore", "Chennai"));
System.out.println("Original list - " + city);
for (int i = 0, j = city.size() - 1; i & lt; j; i++) {
city.add(i, city.remove(j));
}
System.out.println("Reversed list - " + city);
}
}
Output
Original list - [Mumbai, Hyderabad, Delhi, Indore, Chennai]
Reversed list - [Chennai, Indore, Delhi, Hyderabad, Mumbai]
2. Reverse a list using add and remove method - Recursive approach
In this approach, we will use add and remove method of List interface. The add() method appends the specified element to the end of this list. Here, we will always delete the value from the index 0 and store it in "t". For each recursive call, value in t is preserved so that after backtrack it gets appended to the list using add method. When the size of list is 1, it starts to backtrack as there is only last element left in the old list.
The syntax of add() and remove method used is as shown below. Here, element is added at end of the list. Whereas, the element will be removed from the specified index.
listobject.add(element);
listobject.remove(index);
Example : In this example, we will take a list of cities. Thereafter, we will use add and remove method of List interface to reverse the list.
// Program to reverse a list in Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Main
{
public static void reverseCity(List l)
{
// Terminating condition for recursive functionchecking
// checking if the list is empty, or only last element is left
if (l == null || l.size() <= 1) {
return;
}
// remove the element from index 0
Temp t = l.remove(0);
// Calling recursive function for remaining values
reverseCity(l);
// insert the top element after backtracking
l.add(t);
}
public static void main(String[] args)
{
// Declaring a list containing cities
List city = new ArrayList<>(Arrays.asList("Mumbai", "Hyderabad", "Delhi","Indore","Chennai"));
System.out.println("Original list - "+city);
// Function call to reverse a list
reverseCity(city);
System.out.println("Reversed list - "+city);
}
}
Output
Original list - [Mumbai, Hyderabad, Delhi, Indore, Chennai]
Reversed list - [Chennai, Indore, Delhi, Hyderabad, Mumbai]
3. Reverse a list using Collections.reverse()
In this approach, we will use reverse method present in the Collections class. This method reverses the order of the elements in the specified list.
As the reverse is a static method, it is called with class name. The syntax of reverse() method is as shown below. Here, List to be reversed is passed as a parameter.
Collections.reverse(List);
Example : In this example, we will take a list of cities. Thereafter, we will call Collections.reverse to reverse the list.
// Program to reverse a list in Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
class Main
{
public static void main(String[] args)
{
// Declaring a list containing cities
List city = new ArrayList<>(Arrays.asList("Mumbai", "Hyderabad", "Delhi","Indore","Chennai"));
System.out.println("Original list - "+city);
// Using Collections.reverse to reverse a list
Collections.reverse(city);
System.out.println("Reversed list - "+city);
}
}
Output
Original list - [Mumbai, Hyderabad, Delhi, Indore, Chennai]
Reversed list - [Chennai, Indore, Delhi, Hyderabad, Mumbai]
4. Reverse a list using ListIterator()
In this approach, we will use size method to set iterator at the end of the list. Thereafter, we will iterate over the list accessing the elements using previous method. In every iteration, we retrieve the element in reverse direction so as to insert an element into the new list. Here, the hasprevious method returns true if the list contains an element before the current iterator position. Thus, the new list will have all the elements inserted into the reverse order.
The syntax of the hasprevious and the previous() method is as shown below. Here, List to be reversed is passed as a parameter.
listiteratorobject.hasprevious()
listiteratorobject.previous()
Example : In this example, we will take a list of cities. Thereafter, we will use size(), hasPrevious and previous methods of ListIterator Interface.
// Program to reverse a list in Java
import java.util.*;
class Main
{
public static void main(String[] args)
{
// Declaring a list containing cities
List city = new ArrayList<>(Arrays.asList("Mumbai", "Hyderabad", "Delhi","Indore","Chennai"));
System.out.println("Original list - "+city);
List rlist = new ArrayList<>();
// initialize a list iterator at the end element
ListIterator i = city.listIterator(city.size());
// iterate in reverse direction
while(i.hasPrevious())
{
// get previous element and insert it into new list
rlist.add(i.previous());
}
System.out.println("Reverse list - "+rlist);
}
}
Output
Original list - [Mumbai, Hyderabad, Delhi, Indore, Chennai]
Reversed list - [Chennai, Indore, Delhi, Hyderabad, Mumbai]
Summary
The knowledge of reversing a list is very useful. In many applications we need to access the list elements in a reverse order. In this tutorial, we covered four different ways to reverse a list in Java with example. All in all, this tutorial, covers everything that you need to know in order to understand reverse a list in Java.
References
List Interface
Collections Class
ListIterator Interface