top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to reverse an arraylist in Java using recursion?

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

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

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);
        arrayList.add(value);
    }
    return arrayList;
}
answer Mar 22, 2016 by Shivam Kumar Pandey
+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);
       }
answer Mar 30, 2016 by Rajan Paswan
Similar Questions
+2 votes

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.

+4 votes

Addition to this,

Can we consider "a car a man a maraca" as palindrome string?
Click here To see similar kind of palindrome strings,
Are they really a palindrome?

...