Java Program to Reverse a List
Last Updated :
12 Sep, 2022
Reversing a list means after reversing the list, the first element should be swapped with the last element, the second element should be swapped with the second last element, and so on till the middle element and the resultant list is the reversed list.
Example:
Input:
"PLATFORM", "LEARNING", "BEST", "THE", "IS", "GFG"
Output:
Reverse order of given List :-
[GFG, IS, THE, BEST, LEARNING, PLATFORM]
We can mainly reverse the list by three ways:
- Recursively
- Using Collections.reverse()
- Using List.add() and List.remove methods
Method 1: Using Recursion
The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function.
Java
// 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
// the list after removing the first element
revlist(list);
// now after the rest of the list has been
// reversed by the upper recursive call,
// add the first value at the end
list.add(value);
}
public static void main(String[] args)
{
System.out.println(
"Reverse order of given List :- ");
List<String> gfg = new ArrayList<>(
Arrays.asList("PLATFORM", "LEARNING", "BEST", "THE", "IS", "GFG"));
revlist(gfg);
System.out.println(gfg);
}
}
OutputReverse order of given List :-
[GFG, IS, THE, BEST, LEARNING, PLATFORM]
Time complexity: O(N) where N is size of list
Auxiliary space: O(N) for call stack
Method 2: Using Collections.reverse()
java.util.Collections.reverse() method is a java.util.Collections class method. It reverses the order of elements in a list passed as an argument.
Java
// Java program to reverse the list
// using Collections.reverse() method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Integer> number = new ArrayList<>(
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
System.out.println(
"Reverse order of given List :- ");
// the number list will be reversed using this method
Collections.reverse(number);
System.out.println(number);
}
}
OutputReverse order of given List :-
[8, 7, 6, 5, 4, 3, 2, 1]
Method 3: Using List.add() + List.remove()
The List.add() method of List interface is used to append the specified element in argument to the end of the list.
The List.remove() method of List interface is used to remove the specified element in argument from the list.
Java
// Java program to reverse the list using List.add()
// and List.remove() method
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Integer> number = new ArrayList<>(
Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
System.out.println(
"Reverse order of given List :- ");
for (int k = 0, j = number.size() - 1; k < j; k++)
{
number.add(k, number.remove(j));
}
System.out.println(number);
}
}
OutputReverse order of given List :-
[8, 7, 6, 5, 4, 3, 2, 1]
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Java Program for Reverse a linked list
Given a pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input: Head of following linked list 1->2->3->4->NULLOutput: Linked list should be changed to, 4->3->2->1->NULL In
3 min read
Reverse Number Program in Java
In Java, reversing a number means that the digit at the first position should be swapped with the last digit, the second digit will be swapped with the second last digit, and so on, till the middle element.Example of reversing a number:Input: n = 357Output: 753Input n = 100Output: 1 ( leading zeros
3 min read
Program to Convert Set to List in Java
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be s
4 min read
Java Program to Replace an Element in a List
List in Java provides a way to store the ordered collection. The list Interface provides various methods to add, delete, or replace items in the List. In this article, we will learn about how we could replace an element in the list in Java. Methods to Replace an Element in a ListThere are 3 ways to
4 min read
Java Program For Reversing A Doubly Linked List
Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes,
5 min read
Java Program to Reverse a String using Stack
The Stack is a linear data structure that follows the LIFO(Last In First Out) principle, i.e, the element inserted at the last is the element to come out first. Approach: Push the character one by one into the Stack of datatype character.Pop the character one by one from the Stack until the stack be
2 min read
Program to Convert List to Stream in Java
The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack class
3 min read
Java Program to Convert List to HashSet
The List interface provides a way to store the ordered collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. The HashSet class permits the null element. The class al
4 min read
Java Program To Reverse Words In A Given String
Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "p
4 min read
Java Program to Access the Part of List as List
A List is an ordered sequence of elements stored together to form a collection. A list can contain duplicate as well as null entries. A list allows us to perform index-based operations, that is additions, deletions, manipulations, and positional access. Java provides an in-built interface <<ja
4 min read