Skip to content

Commit 97f9f92

Browse files
feat(translation): Added 2.addTwoNumbers.en.md (#373)
* Added 1.TwoSum.en.md * Update problems/1.TwoSum.en.md/Solution Co-authored-by: lucifer <[email protected]> * Update problems/1.TwoSum.en.md/keypoint Co-authored-by: lucifer <[email protected]> * - Added twosum.en.md link in README.en.md - Changed the text in () from 'Not Translated Yet' to 'Transaltion in Progress' - Added check emoji at the end of tranlasted problems' titles * Updated the title of preview as well * Update README.en.md * Added translation for problem 2 * Accepted incoming change Co-authored-by: lucifer <[email protected]>
1 parent 7bca15a commit 97f9f92

File tree

2 files changed

+190
-1
lines changed

2 files changed

+190
-1
lines changed

README.en.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ The data structures mainly include:
111111

112112
> Here only lists some **representative problems** but not all.
113113
114+
<<<<<<< HEAD
115+
116+
=======
117+
>>>>>>> upstream/master
114118
#### Easy (Translation in Progress)
115119
- [0001.TwoSum](./problems/1.TwoSum.en.md)🆕✅
116120
- [0020.Valid Parentheses](./problems/20.validParentheses.md)
@@ -147,7 +151,7 @@ The data structures mainly include:
147151

148152
#### Medium (Translation in Progress)
149153

150-
- [0002. Add Two Numbers](./problems/2.addTwoNumbers.md)
154+
- [0002. Add Two Numbers](./problems/2.addTwoNumbers.en.md)
151155
- [0003. Longest Substring Without Repeating Characters](./problems/3.longestSubstringWithoutRepeatingCharacters.md)
152156
- [0005.longest-palindromic-substring](./problems/5.longest-palindromic-substring.md)
153157
- [0011.container-with-most-water](./problems/11.container-with-most-water.md)
@@ -170,7 +174,11 @@ The data structures mainly include:
170174
- [0062.unique-paths](./problems/62.unique-paths.md)
171175
- [0073.set-matrix-zeroes](./problems/73.set-matrix-zeroes.md)
172176
- [0075.sort-colors](./problems/75.sort-colors.md)
177+
<<<<<<< HEAD
178+
- [0078.subsets](./problems/78.subsets.md)
179+
=======
173180
- [0078.subsets](./problems/78.subsets-en.md)
181+
>>>>>>> upstream/master
174182
- [0079.word-search](./problems/79.word-search-en.md)
175183
- [0086.partition-list](./problems/86.partition-list.md)
176184
- [0090.subsets-ii](./problems/90.subsets-ii-en.md)
@@ -216,7 +224,10 @@ The data structures mainly include:
216224
- [0516.longest-palindromic-subsequence](./problems/516.longest-palindromic-subsequence.md)
217225
- [0518.coin-change-2](./problems/518.coin-change-2.md)
218226
- [0547.friend-circles](./problems/547.friend-circles-en.md) 🆕✅
227+
<<<<<<< HEAD
228+
=======
219229
- [0560.subarray-sum-equals-k](./problems/560.subarray-sum-equals-k.en.md)
230+
>>>>>>> upstream/master
220231
- [0609.find-duplicate-file-in-system](./problems/609.find-duplicate-file-in-system.md)
221232
- [0875.koko-eating-bananas](./problems/875.koko-eating-bananas.md)
222233
- [0877.stone-game](./problems/877.stone-game.md)
@@ -229,6 +240,10 @@ The data structures mainly include:
229240
- [1371.find-the-longest-substring-containing-vowels-in-even-counts](./problems/1371.find-the-longest-substring-containing-vowels-in-even-counts.en.md) 🆕✅
230241

231242

243+
<<<<<<< HEAD
244+
245+
=======
246+
>>>>>>> upstream/master
232247
#### Hard (Translation in Progress)
233248

234249
- [0004.median-of-two-sorted-array](./problems/4.median-of-two-sorted-array.md) 🆕

problems/2.addTwoNumbers.en.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
![2.addTwoNumbers](../assets/2.addTwoNumbers.gif)
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

Comments
 (0)