|
| 1 | +## Problem |
| 2 | +https://leetcode.com/problems/add-two-numbers/description/ |
| 3 | + |
| 4 | +## Problem Description |
| 5 | +``` |
| 6 | +You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. |
| 7 | +
|
| 8 | +You may assume the two numbers do not contain any leading zero, except the number 0 itself. |
| 9 | +
|
| 10 | +Example |
| 11 | +
|
| 12 | +Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) |
| 13 | +Output: 7 -> 0 -> 8 |
| 14 | +Explanation: 342 + 465 = 807. |
| 15 | +
|
| 16 | +``` |
| 17 | +## Solution |
| 18 | + |
| 19 | +Define a new variable `carried` that represents the carry value during the calculation, and a new linked list |
| 20 | +Traverse the two linked lists from the start to the end simultaneously, and calculate the sum of node value from each linked list. The sum of the result and `carried` would be appended as a new node to the end of the new linked list. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +(Image Reference: https://github.com/MisterBooo/LeetCodeAnimation) |
| 25 | + |
| 26 | +## Key Point Analysis |
| 27 | + |
| 28 | +1. The characteristics and application of this data structure - linked list |
| 29 | + |
| 30 | +2. Define a variable named `carried` to replace the role of carry-over, calculate `carried` after each sum and apply it to the next round's calculation |
| 31 | + |
| 32 | +## Code |
| 33 | +* Language Support: JS, C++ |
| 34 | + |
| 35 | +JavaScript: |
| 36 | +```js |
| 37 | +/** |
| 38 | + * Definition for singly-linked list. |
| 39 | + * function ListNode(val) { |
| 40 | + * this.val = val; |
| 41 | + * this.next = null; |
| 42 | + * } |
| 43 | + */ |
| 44 | +/** |
| 45 | + * @param {ListNode} l1 |
| 46 | + * @param {ListNode} l2 |
| 47 | + * @return {ListNode} |
| 48 | + */ |
| 49 | +var addTwoNumbers = function(l1, l2) { |
| 50 | + if (l1 === null || l2 === null) return null |
| 51 | + |
| 52 | + // using dummyHead can simplify linked list's calculation, dummyHead.next points to the new linked list |
| 53 | + let dummyHead = new ListNode(0) |
| 54 | + let cur1 = l1 |
| 55 | + let cur2 = l2 |
| 56 | + let cur = dummyHead // cur is for the calculation in new linked list |
| 57 | + let carry = 0 // carry-over symbol |
| 58 | + |
| 59 | + while (cur1 !== null || cur2 !== null) { |
| 60 | + let val1 = cur1 !== null ? cur1.val : 0 |
| 61 | + let val2 = cur2 !== null ? cur2.val : 0 |
| 62 | + let sum = val1 + val2 + carry |
| 63 | + let newNode = new ListNode(sum % 10) // the result of sum%10 ranges from 0 to 9, which is the value of the current digit |
| 64 | + carry = sum >= 10 ? 1 : 0 // sum>=10, carry=1, so carry-over exists here |
| 65 | + cur.next = newNode |
| 66 | + cur = cur.next |
| 67 | + |
| 68 | + if (cur1 !== null) { |
| 69 | + cur1 = cur1.next |
| 70 | + } |
| 71 | + |
| 72 | + if (cur2 !== null) { |
| 73 | + cur2 = cur2.next |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (carry > 0) { |
| 78 | + // If there's still carry-over in the end, then add a new node |
| 79 | + cur.next = new ListNode(carry) |
| 80 | + } |
| 81 | + |
| 82 | + return dummyHead.next |
| 83 | +}; |
| 84 | +``` |
| 85 | +C++ |
| 86 | +> C++ code is slightly different from the JavaScript code above: the step that checks whether carry equals to 0 is put in the while-loop. |
| 87 | +```c++ |
| 88 | +/** |
| 89 | + * Definition for singly-linked list. |
| 90 | + * struct ListNode { |
| 91 | + * int val; |
| 92 | + * ListNode *next; |
| 93 | + * ListNode(int x) : val(x), next(NULL) {} |
| 94 | + * }; |
| 95 | + */ |
| 96 | +class Solution { |
| 97 | +public: |
| 98 | + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { |
| 99 | + ListNode* ret = nullptr; |
| 100 | + ListNode* cur = nullptr; |
| 101 | + int carry = 0; |
| 102 | + while (l1 != nullptr || l2 != nullptr || carry != 0) { |
| 103 | + carry += (l1 == nullptr ? 0 : l1->val) + (l2 == nullptr ? 0 : l2->val); |
| 104 | + auto temp = new ListNode(carry % 10); |
| 105 | + carry /= 10; |
| 106 | + if (ret == nullptr) { |
| 107 | + ret = temp; |
| 108 | + cur = ret; |
| 109 | + } |
| 110 | + else { |
| 111 | + cur->next = temp; |
| 112 | + cur = cur->next; |
| 113 | + } |
| 114 | + l1 = l1 == nullptr ? nullptr : l1->next; |
| 115 | + l2 = l2 == nullptr ? nullptr : l2->next; |
| 116 | + } |
| 117 | + return ret; |
| 118 | + } |
| 119 | +}; |
| 120 | +``` |
| 121 | +## Extension |
| 122 | +The singly-linked list also has a recursive structure based on its definition. Therefore, the recursive apporach works on reversing a linked list, as well. |
| 123 | +> Because a singly-linked list is a linear data structure, the recursive approach means that the use of stack would also be linear. When the linked list's length reaches a certain level, the recursion would result in a stack overflow. Therefore, using recursion to manipulate a linked list is not recommended in reality. |
| 124 | +
|
| 125 | +### Description |
| 126 | +
|
| 127 | +1. Add up the first node of two linked lists, and covert the result to a number between 0 and 10, record the carry-over as well. |
| 128 | +2. Proceed to add up the two linked lists after the first node with carry-over recursively |
| 129 | +3. Point the next of the head node from the first step to the linked list returned from the second step |
| 130 | +
|
| 131 | +### C++ Implementation |
| 132 | +```C++ |
| 133 | +// Normal recursion |
| 134 | +class Solution { |
| 135 | +public: |
| 136 | + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { |
| 137 | + return addTwoNumbers(l1, l2, 0); |
| 138 | + } |
| 139 | + |
| 140 | +private: |
| 141 | + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2, int carry) { |
| 142 | + if (l1 == nullptr && l2 == nullptr && carry == 0) return nullptr; |
| 143 | + carry += (l1 == nullptr ? 0 : l1->val) + (l2 == nullptr ? 0 : l2->val); |
| 144 | + auto ret = new ListNode(carry % 10); |
| 145 | + ret->next = addTwoNumbers(l1 == nullptr ? l1 : l1->next, |
| 146 | + l2 == nullptr ? l2 : l2->next, |
| 147 | + carry / 10); |
| 148 | + return ret; |
| 149 | + } |
| 150 | +}; |
| 151 | +// (Similiar) Tail recursion |
| 152 | +class Solution { |
| 153 | +public: |
| 154 | + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { |
| 155 | + ListNode* head = nullptr; |
| 156 | + addTwoNumbers(head, nullptr, l1, l2, 0); |
| 157 | + return head; |
| 158 | + } |
| 159 | + |
| 160 | +private: |
| 161 | + void addTwoNumbers(ListNode*& head, ListNode* cur, ListNode* l1, ListNode* l2, int carry) { |
| 162 | + if (l1 == nullptr && l2 == nullptr && carry == 0) return; |
| 163 | + carry += (l1 == nullptr ? 0 : l1->val) + (l2 == nullptr ? 0 : l2->val); |
| 164 | + auto temp = new ListNode(carry % 10); |
| 165 | + if (cur == nullptr) { |
| 166 | + head = temp; |
| 167 | + cur = head; |
| 168 | + } else { |
| 169 | + cur->next = temp; |
| 170 | + cur = cur->next; |
| 171 | + } |
| 172 | + addTwoNumbers(head, cur, l1 == nullptr ? l1 : l1->next, l2 == nullptr ? l2 : l2->next, carry / 10); |
| 173 | + } |
| 174 | +}; |
0 commit comments