|
| 1 | +/** |
| 2 | + * @see {@link https://leetcode.com/problems/add-two-numbers/ 2. Add Two Numbers} |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * EXPLAIN |
| 7 | + * Two linked lists; each has one single-digit int as val |
| 8 | + * Reverse order of digits to get one number: |
| 9 | + * 9 -> 1 -> 4 becomes 419 |
| 10 | + * 8 -> 3 -> 7 becomes 738 |
| 11 | + * Add those two reversed numbers and then return as LL! |
| 12 | + * |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * APPROACH |
| 18 | + * |
| 19 | + * Values as powers of 10 |
| 20 | + * create total val to store sums |
| 21 | + * |
| 22 | + * create power counter |
| 23 | + * |
| 24 | + * WHILE first list next is not null |
| 25 | + * multiply node val x 10^powerCounter |
| 26 | + * END |
| 27 | + * |
| 28 | + * reset |
| 29 | + * |
| 30 | + * WHILE second list next is not null |
| 31 | + * multiply node val x 10^powerCounter |
| 32 | + * END |
| 33 | + * |
| 34 | + * return total |
| 35 | + */ |
| 36 | +/** |
| 37 | + * Definition for singly-linked list. |
| 38 | + * function ListNode(val, next) { |
| 39 | + * this.val = (val===undefined ? 0 : val) |
| 40 | + * this.next = (next===undefined ? null : next) |
| 41 | + * } |
| 42 | + */ |
| 43 | + |
| 44 | +/** |
| 45 | + * 1st ATTEMPT |
| 46 | + * 53minutes FAIL |
| 47 | + */ |
| 48 | +/** |
| 49 | + * @param {ListNode} l1 |
| 50 | + * @param {ListNode} l2 |
| 51 | + * @return {ListNode} |
| 52 | + */ |
| 53 | +var addTwoNumbers = function (l1, l2) { |
| 54 | + let total = 0; |
| 55 | + let power = 0; |
| 56 | + |
| 57 | + while (l1.next) { |
| 58 | + const currSum = l1.val * Math.pow(10, power); |
| 59 | + total += currSum; |
| 60 | + power++; |
| 61 | + |
| 62 | + // advance |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +/** |
| 67 | + * Solution used |
| 68 | + */ |
| 69 | +/** |
| 70 | + * Definition for singly-linked list. |
| 71 | + * function ListNode(val, next) { |
| 72 | + * this.val = (val===undefined ? 0 : val) |
| 73 | + * this.next = (next===undefined ? null : next) |
| 74 | + * } |
| 75 | + */ |
| 76 | +/** |
| 77 | + * @param {ListNode} l1 |
| 78 | + * @param {ListNode} l2 |
| 79 | + * @return {ListNode} |
| 80 | + */ |
| 81 | +var addTwoNumbers = function (l1, l2) { |
| 82 | + // Create a dummy head node because |
| 83 | + // we need a pointer to the first node in the solution. |
| 84 | + // See how we return headNode.next at the end of the code. |
| 85 | + let headNode = new ListNode("dummy_node"); |
| 86 | + |
| 87 | + // Keep track of the current node so that we can easily add a new node at the end |
| 88 | + let currNode = headNode; |
| 89 | + |
| 90 | + // Value carried over from previous addition (is either 0 or 1) |
| 91 | + let carry = 0; |
| 92 | + |
| 93 | + // Same as: while (l1 != null || l2 != null || carry != 0) |
| 94 | + while (l1 || l2 || carry) { |
| 95 | + let sum = carry; |
| 96 | + if (l1) { |
| 97 | + sum += l1.val; |
| 98 | + l1 = l1.next; |
| 99 | + } |
| 100 | + if (l2) { |
| 101 | + sum += l2.val; |
| 102 | + l2 = l2.next; |
| 103 | + } |
| 104 | + if (sum >= 10) { |
| 105 | + // If the sum is two digit, |
| 106 | + // we need to carry over 1 to the next loop |
| 107 | + carry = 1; |
| 108 | + sum -= 10; |
| 109 | + } else { |
| 110 | + carry = 0; |
| 111 | + } |
| 112 | + // Add a new node to our solution (linked list) |
| 113 | + currNode.next = new ListNode(sum); |
| 114 | + |
| 115 | + // advance the current node |
| 116 | + currNode = currNode.next; |
| 117 | + } |
| 118 | + |
| 119 | + return headNode.next; |
| 120 | +}; |
| 121 | + |
| 122 | +/** |
| 123 | + * READ 05:46 |
| 124 | + * EXPLAIN 03:40 09:27 |
| 125 | + * APPROACH 22:52 32:20 |
| 126 | + * CODE [FAIL] 22:19 54:39 |
| 127 | + * TEST |
| 128 | + * OPTIMIZE |
| 129 | + */ |
0 commit comments