The following code for above requirement. However I am not getting proper output. There is problem with input list that I am passing in recursion Answer The problem is not, like you said in your comment, that the list ip keeps elements of the previous function call. The problem is that you don’t create a new arraylist as ip parameter