Easy Java Solution || Using stack
Intuition\n Describe your first thoughts on how to solve this problem. \nTo solve the problem, you can start by converting the array num into an integer, then a
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo solve the problem, you can start by converting the array num into an integer, then add k to it, and finally convert the result back into an array. However, since the constraints are quite large, you need to be careful about the implementation to avoid integer overflow.\n\nOne way to avoid integer overflow is to add k to the least significant digit of num, then compute the carry and propagate it to the more significant digits until there is no more carry. To convert the result back into an array, you can use a stack to store the digits in reverse order.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->Initialize a stack to store the digits of the result in reverse order, and set the carry to k.\nIterate over the digits of the input array num from right to left, adding each digit to the carry and updating the carry accordingly. If the input array has more digits than k, the remaining digits are effectively treated as zeros.\nAfter processing all the digits of num, or if there is still a carry, continue the iteration by adding the carry to the next digit (which may be zero), and updating the carry accordingly.\nCompute the least significant digit of the result by taking the remainder of the sum modulo 10, and push it onto the stack.\nDivide the sum by 10 to update the carry, and repeat the process for the next digit.\nAfter processing all the digits, the stack contains the digits of the result in reverse order, so we create a new list and pop the digits from the stack one by one, adding them to the list.\nReturn the list as the final result.\n\n\n# Complexity\n- Time complexity:O(max(N, log K))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(max(N, log K))\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public List<Integer> addToArrayForm(int[] num, int k) {\n        // Initialize a stack to store the digits of the result in reverse order, and set the carry to k.\n        Stack<Integer> stack = new Stack<>();\n        int carry = k;\n        \n        // Iterate over the digits of the input array num from right to left, adding each digit to the carry and updating the carry accordingly.\n        int i = num.length - 1;\n        while (i >= 0 || carry > 0) {\n            if (i >= 0) {\n                carry += num[i];\n            }\n            \n            // Compute the least significant digit of the result by taking the remainder of the sum modulo 10, and push it onto the stack.\n            stack.push(carry % 10);\n            \n            // Divide the sum by 10 to update the carry, and repeat the process for the next digit.\n            carry /= 10;\n            i--;\n        }\n        \n        // Create a new list to store the digits of the result in the correct order, and pop the digits from the stack one by one, adding them to the list.\n        List<Integer> result = new ArrayList<>();\n        while (!stack.isEmpty()) {\n            result.add(stack.pop());\n        }\n        \n        // Return the list as the final result.\n        return result;\n    }\n}\n\n```
['Java']