[Java/C++/Python] Take K itself as a Carry
Explanation\nTake K as a carry.\nAdd it to the lowest digit,\nUpdate carry K,\nand keep going to higher digit.\n\n\n## Complexity\nInsert will take O(1) time or
## **Explanation**\nTake `K` as a carry.\nAdd it to the lowest digit,\nUpdate carry `K`,\nand keep going to higher digit.\n<br>\n\n## **Complexity**\nInsert will take `O(1)` time or `O(N)` time on shifting, depending on the data stucture.\nBut in this problem `K` is at most 5 digit so this is restricted.\nSo this part doesn\'t matter.\n\nThe overall time complexity is `O(N)`.\nFor space I\'ll say `O(1)`\n<br>\n\n**Java**\n```java\n    public List<Integer> addToArrayForm(int[] A, int K) {\n        List<Integer> res = new LinkedList<>();\n        for (int i = A.length - 1; i >= 0; --i) {\n            res.add(0, (A[i] + K) % 10);\n            K = (A[i] + K) / 10;\n        }\n        while (K > 0) {\n            res.add(0, K % 10);\n            K /= 10;\n        }\n        return res;\n    }\n```\n**Java**\nWith one loop.\n```java\n    public List<Integer> addToArrayForm(int[] A, int K) {\n        List res = new LinkedList<>();\n        for (int i = A.length - 1; i >= 0 || K > 0; --i) {\n            res.add(0, (i >= 0 ? A[i] + K : K) % 10);\n            K = (i >= 0 ? A[i] + K : K) / 10;\n        }\n        return res;\n    }\n```\n\n**C++:**\n```cpp\n    vector<int> addToArrayForm(vector<int> A, int K) {\n        for (int i = A.size() - 1; i >= 0 && K > 0; --i) {\n            A[i] += K;\n            K = A[i] / 10;\n            A[i] %= 10;\n        }\n        while (K > 0) {\n            A.insert(A.begin(), K % 10);\n            K /= 10;\n        }\n        return A;\n    }\n```\n\n**Python:**\n```py\n    def addToArrayForm(self, A, K):\n        for i in range(len(A) - 1, -1, -1):\n            K, A[i] = divmod(A[i] + K, 10)\n        return [int(i) for i in str(K)] + A if K else A\n```\n
[]