(Java) Problem 16.5 simpler solution

#1

Hello!

Wouldn’t this solution be a bit simpler than the reference solution?

static List<LinkedList<Integer>> gen(int k, int n){
        List<LinkedList<Integer>> result = new ArrayList<>();
        gen(result, new LinkedList<>(), k,n);
        return result;
}
static void gen(List<LinkedList<Integer>> result, LinkedList<Integer> tail, int k, int max){
        if (k == 0){
                result.add(new LinkedList<>(tail));
                return;
        }
        if (k > max){
                return;
        }
        gen(result, tail, k, max-1);
        tail.addFirst(max);
        gen(result, tail, k-1, max-1);
        tail.removeFirst();
}
0 Likes