简体   繁体   中英

Reversing elements of a list using recursion

The aim is to write a code which reverses the elements of a list with help of recursion.

public static List<Integer> reverse(List<Integer> input) {
    if (input.isEmpty()) {
        return new LinkedList<Integer>();
    } else {
        List<Integer> output = new LinkedList<Integer>();
        output.add(((LinkedList<Integer>) input).removeLast());
        reverse(input);
        return output;
    }
}

Unfortunately I am getting only the first element right, the rest of the list just does not show up. What am I missing?

How it was mentioned in comments, you need a second parameter and probably don't need return value:

public static void reverse(List<Integer> input, List<Integer> output) {
    if (input.isEmpty()) {
        return;
    }
    output.add(((LinkedList<Integer>) input).removeLast());
    reverse(input, output);
}

Usage:

List<Integer> input = new LinkedList<>();
// fill it with values
List<Integer> output = new LinkedList<>();

reverse(input, output);
System.out.println(output);

You can do this like in below code. Note that I'm using removeFirst() method.

import java.util.LinkedList;
import java.util.List;

public class Reverse {

  public static List<Integer> reverse(List<Integer> input) {
    if (input.isEmpty()) {
      return new LinkedList<Integer>();
    } else {
      Integer first = ((LinkedList<Integer>) input).removeFirst();
      List<Integer> output = reverse(input);
      output.add(first);
      return output;
    }
  }

  public static void main(String[] args) {

    List<Integer> input = new LinkedList<>();
    input.add(15);
    input.add(37);
    input.add(26);
    input.add(18);
    input.add(31);

    System.out.println("Input  : " + input);
    System.out.println("Output : " + reverse(input));
  }
}

I am using for convinience a Stack for its pop method.

public static List<Integer> reverse(List<Integer> input) {
    Stack<Integer> stack = new Stack();
    stack.addAll(input);
    return reverse(stack,new LinkedList<>());
  }

  public static List<Integer> reverse(Stack<Integer> input,LinkedList<Integer> output) {
     if (input.isEmpty()) {
       return output;
     }
     output.addFirst(input.pop());
     reverse(input, output);
    return output;
  }

If you want to skip the re-addition of the elements you need to maintain an index, or use a LinkedList which is aware of the first and the last elements. Here it is with maintaining index and a pure List api:

public static List<Integer> reverse(List<Integer> input) {


     return reverse(input,new LinkedList<>(),0);
  }

  public static List<Integer> reverse(List<Integer> input,LinkedList<Integer> output,int index) {
     if (index == input.size()) {
       return output;
     }
     output.addFirst(input.get(index));
     reverse(input, output,++index);
    return output;
  }
// This will work for you

    public static void main(String[] args) {
            List<Integer> input = new LinkedList<Integer>();
            List<Integer> output = new LinkedList<Integer>();
            input.add(5);
            input.add(1);
            input.add(3);
            int size = input.size();
            System.out.println(reverse(input, size, output));

        }

        public static List<Integer> reverse(List<Integer> input, int sizeOfInput, List<Integer> output) {

            if (sizeOfInput > 0) {
                output.add(((LinkedList<Integer>) input).removeLast());
                sizeOfInput--;
                reverse(input, sizeOfInput, output);
            }
            return output;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM