Working java code not getting accepted

public class Solution {
public void nextPermutation(ArrayList<Integer> a) {
    swap(a, 0, 1);
    if (!swapped) {
        int first = 0, last = a.size() - 1;
        while (first < last) {
            int temp = a.get(first);
            a.set(first, a.get(last));
            a.set(last, temp);
        }
    }
}
static boolean swapped = false;

static void swap(ArrayList<Integer> a, int currIndex, int nextIndex) {
    if (nextIndex < a.size()) {
        swap(a, currIndex + 1, nextIndex + 1);
        if (!swapped) {
            if (a.get(currIndex) < a.get(nextIndex)) {
                int temp = a.get(nextIndex);
                a.set(nextIndex, a.get(currIndex));
                a.set(currIndex, temp);
                swapped = true;
                Collections.sort(a.subList(nextIndex, a.size()));
            }
        }
    }
}
}

My solution in java is working fine but getting failure for the test case A : [ 251, 844, 767, 778, 658, 337, 10, 252, 632, 262, 707, 506, 701, 475, 410, 696, 631, 903, 516, 149, 344, 101, 42, 891, 991 ]

Your function returned the following

251 844 767 778 658 337 10 252 632 262 707 506 701 475 410 696 631 903 516 149 344 101 42 891 991

But I am getting correct output for the test case. Can someone help ?

Click here to start solving coding interview questions