how to reverse an arraylist in java using recursion

public ArrayList<Object> reverse(ArrayList<Object> arrayList) {
    if(arrayList.size() > 1) {                   
        Object value = arrayList.remove(0);
        reverse(arrayList);
        arrayList.add(value);
    }
    return arrayList;
}

4.17
6

                                    import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/*
 * Java Program to demonstrate how to reverse a List.
 * In this example, you will see two ways to reverse a List,
 * first, using Collections.reverse() method and second
 * by writing your own method using recursion. 
 */
public class TestSolution {

    public static void main(String args[]) {

        List&lt;String&gt; books = new ArrayList&lt;&gt;();
        books.add(&quot;Beautiful Code&quot;);
        books.add(&quot;Clean Code&quot;);
        books.add(&quot;Working Effectively with Legacy Code&quot;);

        System.out.println(&quot;Original order of List: &quot; + books);

        // Easy way to reverse a List in Java, use Collections.reverse()
        // method, use this to reverse ArrayList or LinkedList in
        // production
        Collections.reverse(books);

        System.out.println(&quot;The reversed List: &quot; + books);

        // Now, let's try to reverse a List using recursion
        List&lt;String&gt; output = reverseListRecursively(books);
        System.out.println(&quot;Reversed list reversed again: &quot; + output);
    }

    /**
     * A recursive algorithm to reverse a List in Java
     *
     * @param list
     * @return
     */
    private static List&lt;String&gt; reverseListRecursively(List&lt;String&gt; list) {
        if (list.size() &lt;= 1) {
            return list;
        }

        List&lt;String&gt; reversed = new ArrayList&lt;&gt;();
        reversed.add(list.get(list.size() - 1)); // last element
        reversed.addAll(reverseListRecursively(list.subList(0, list.size() - 1)));
        return reversed;
    }

}

Output
Original order of List: [Beautiful Code, Clean Code, Working Effectively with Legacy Code]
The reversed List: [Working Effectively with Legacy Code, Clean Code, Beautiful Code]
Reversed list reversed again: [Beautiful Code, Clean Code, Working Effectively with Legacy Code]

4.17 (6 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source