This will malloc just the right amount of memory. I know it's "just" 1 extra int, but anyway...

```
int* plusOne(int* digits, int digitsSize, int* returnSize) {
int i = (digitsSize) - 1; // start from back
int* newDigits = NULL;
// if input is 0 don't bother
if(!digits || digitsSize < 1) return NULL;
while(i >= 0)
{
if(digits[i] == 9)
{
digits[i] = 0;
i--;
// continue loop because we had to "carry" the 1
}
else
{
digits[i] = digits[i] + 1;
i--;
break; // stop the while loop because no "carry" needed
}
}
if(digits[0] == 0)
{
// if first element is 0 then that means it was originally 9 and the 1 "carried". Need to malloc larger array.
*returnSize = digitsSize + 1;
newDigits = (int*)malloc(sizeof(int) * (*returnSize));
memcpy(newDigits + 1, digits, sizeof(int) * (digitsSize));
newDigits[0] = 1;
}
else
{
// first element did not have to increase, so original array size is still valid
*returnSize = digitsSize;
newDigits = (int*)malloc(sizeof(int) * (*returnSize));
memcpy(newDigits, digits, sizeof(int) * (digitsSize));
}
return newDigits;
}
```