Simple java solution using collections framework!


#1
public class Solution {
public ArrayList<Integer> nextPermutation(ArrayList<Integer> A) {
    List<Integer> sublist = new ArrayList<Integer>();
    List<Integer> li = new ArrayList<>(A);
	Collections.sort(li);
	Collections.reverse(li);
	
	if(li.equals(A)){
	    Collections.sort(A);
	    return A;
	}
	
    for(int i= A.size()-1; i>0; i--){
        if(A.get(i-1) < A.get(i)){
            sublist = A.subList(i,A.size());
            int j = 0;
            int min = Integer.MAX_VALUE;
            while(j<sublist.size()){
                if(sublist.get(j)>A.get(i-1)){
                    min = Math.min(sublist.get(j), min);
                }
                j++;
            }
            
            Collections.swap(A, i-1, sublist.indexOf(min) + i);
            
            sublist = A.subList(i, A.size());
            Collections.sort(sublist);
            j = 0;
            int p = i;
            while(j<sublist.size() && p<A.size()){
                A.set(p, sublist.get(j));
                j++;
                p++;
            }
            break;
        }
    }
    
    return A;
}

}