I am trying to learn recursion by creating a permutation of an ArrayList:
{1,2,3}
but the concept of recursive calls just keeps going over my head. I know how to do an iterative solution. but is there a systematic way to convert my iterative solution to recursive?
private static ArrayList<ArrayList<Integer>> permutate(ArrayList<Integer> list) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
result.add(new ArrayList<Integer>());
for (int i = 0; i < list.size(); i++) {
ArrayList<ArrayList<Integer>> current = new ArrayList<ArrayList<Integer>>();
for (ArrayList<Integer> l : result) {
for (int j = 0; j < l.size()+1; j++) {
l.add(j, list.get(i));
ArrayList<Integer> temp = new ArrayList<Integer>(l);
current.add(temp);
l.remove(j);
}
}
result = new ArrayList<ArrayList<Integer>>(current);
}
return result;
}