Skip to content
Related Articles

Related Articles

Java Program to Reverse a List
  • Difficulty Level : Hard
  • Last Updated : 19 Jan, 2021

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:

  1. Recursively
  2. Using Collections.reverse()
  3. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 fisrt 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);
    }
}

chevron_right


Output

Reverse order of given List :- 
[GFG, IS, THE, BEST, LEARNING, PLATFORM]

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output

Reverse 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to reverse the list usinf 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);
    }
}

chevron_right


Output

Reverse order of given List :- 
[8, 7, 6, 5, 4, 3, 2, 1]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :