[Java] 3 different approach || 55ms to 4ms runtime
Hii,\nIntuition :\n\nBASIC IDEA\n1.  Basic Idea behind this implementation is to add the num array element one by one with the k\neg:\nnum[] = {1, 2, 3} and k =
Hii,\n**Intuition :**\n\nBASIC IDEA\n1.  Basic Idea behind this implementation is to add the num array element one by one with the k\neg:\nnum[] = {1, 2, 3} and k = 45;\n\n* \t1st Iteration:\nk += num[len--] --> k = k + num[2] --> k = 45 + 3 --> k = 48\nlist.addFirst(k % 10) --> list.addFirst(48 % 10) --> list.addFirst(8) --> [8]\nk /= 10 --> k = k / 10 --> 48 / 10 --> k = 4\n\n* 2nd Iteration:\nk += num[len--] --> k = k + num[1] --> k = 4 + 2 --> k = 6\nlist.addFirst(k % 10) --> list.addFirst(6 % 10) --> list.addFirst(6) --> [6, 8]\nk /= 10 --> k = k / 10 --> 6 / 10 --> k = 0\n\n* 3rd Iteration:\nk += num[len--] --> k = k + num[0] --> k = 0 + 1 --> k = 1\nlist.addFirst(k % 10) --> list.addFirst(1 % 10) --> list.addFirst(1) --> [1, 6, 8] <--- Desired Output\nk /= 10 --> k = k / 10 --> 1 / 10 --> k = 0\n\n\t//  credit @akshayaamar05\n\t\nAt the beginning i was getting **55ms** runtime for submission which was slower than 80% but changing my list it was mitigated to just **4ms** which is faster than ***98%*** submission.\nso, let just look how it was done\n1. First using ArrayLIst we are appending the element at the beginning of ArrayList \n2. taking lot of time process \n3.  runtime = 55ms.\n```\npublic List<Integer> addToArrayForm(int[] num, int k) {\n        ArrayList<Integer> list = new ArrayList<Integer>();               \n        int len = num.length-1;\n        while( len >=0 || k!=0){\n            if( len >= 0){\n                k += num[len--];\n            }\n            list.add(0,k % 10);        // using  .add(int index, E value); method to add element at the beginning of arraylist                   \n            k /= 10;\n        }    \n      return list;\n    }\n```\n\nUsing LinkedList\n* using linkedList provide the method addFirst(value) which consumes less time \n* runtime = 7ms.\n* space = 64 MB, less than only 12%.\n```\npublic List<Integer> addToArrayForm(int[] num, int k) {\n        LinkedList<Integer> list = new LinkedList<Integer>();               \n        int len = num.length-1;\n        while( len >=0 || k!=0){\n            if( len >= 0){\n                k += num[len];\n                len--;\n            }\n            list.addFirst(k % 10);                          // adding remender to beginning of LinkedList \n            k /= 10;\n        }                \n      return list;\n    }\n```\n![image](https://assets.leetcode.com/users/images/1654c6e4-5259-4fb5-85e6-02c8f27c81ae_1657501263.0771966.png)\n\n\n\nAgain using ArrayList but with different approach \n* As we have seen above it is least efficient it is because it has to take some extra time to process the method add(int index, E value)\n* but can avoid this and can achieve most efficient solution for both space and time \n* Runtime = 4ms\n* space = 43 MB lesser than **99.39%**.\n\n```\n public List<Integer> addToArrayForm(int[] num, int k) {\n        ArrayList<Integer> list = new ArrayList<Integer>();               \n        int len = num.length-1;\n        while( len >=0 || k!=0){\n            if( len >= 0){\n                k += num[len];\n                len--;\n            }\n            list.add(k % 10);                      // using normal add method which add element at the end of list.     \n            k /= 10;\n        }\n        Collections.reverse(list);         // using collections reverse method to reverse our arraylist         \n      return list;\n    }\n```\n\n![image](https://assets.leetcode.com/users/images/38420d40-d2d6-4e8d-8eb7-28dbfdd474ff_1657501703.7822409.png)\nThanks for Reading!\nKeep Grinding!\nPlease Upvote, if you find helpful.\n\n\n
['Java']