If they allowed to use an ArrayList as the return type then we would not need the newDigits array then.

public int[] plusOne(int[] digits)

{

int val=0,flag=0,i;

for(i=digits.length-1;i>=0;i--)

{

val=digits[i]+1;

digits[i]=val%10;

if(val>=10)

continue;

else

{

break;

}

}

if(i<0)

{

int[] newDigits=new int[digits.length+1];

newDigits[0]=1;

return newDigits;

}

return digits;

` }`