How to reverse an arraylist in Java using recursion?

+1 vote
651 views
How to reverse an arraylist in Java using recursion?
posted Mar 21, 2016

Method to reverse ArrayList using recursive method,

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

Method to reverse an number in Java

`````` public  void reverse(String args[])
{
int n, rev = 0;

System.out.println("Enter the number");
Scanner in = new Scanner(System.in);
n = in.nextInt();

while( n != 0 )
{
rev = rev * 10;
rev = rev + n%10;
n = n/10;
}

System.out.println("Reverse of entered number is "+rev);
}
``````
Similar Questions

I need to write a function to flat nested lists as this one:

``````[[1,2,3],4,5,[6,[7,8]]]
``````

To the result:

``````[1,2,3,4,5,6,7,8]
``````

So I searched for example code and I found this one that uses recursion (that I don't understand):

``````def flatten(l):
ret = []
for i in l:
if isinstance(i, list) or isinstance(i, tuple):
ret.extend(flatten(i)) #How is flatten(i) evaluated?
else:
ret.append(i)
return ret
``````

So I know what recursion is, but I don't know how is

`````` flatten(i)
``````

evaluated, what value does it returns?

+1 vote

Write a recursive function:

``````int sum( int x, int max )
{
/* complete the code */
}
``````

that calculates the sum of the numbers from x to max (inclusive). For example, sum (4, 7) would compute 4 + 5 + 6 + 7 and return the value 22.

Note: The function must be recursive.

Can we consider `"a car a man a maraca"` as palindrome string?