-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2.cpp
More file actions
44 lines (44 loc) · 1.14 KB
/
Copy path2.cpp
File metadata and controls
44 lines (44 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
int lenL1 = 0, lenL2 = 0;
ListNode *tempL1 = l1, *tempL2 = l2, *ret = l1, *tempRet = nullptr;
while (tempL1) {
++lenL1;
tempL1 = tempL1->next;
}
while (tempL2) {
++lenL2;
tempL2 = tempL2->next;
}
if (lenL1 < lenL2) {
ListNode *temp = l1;
l1 = l2;
ret = l2;
l2 = temp;
}
int sur = 0;
while (l1 && l2) {
if (lenL1 == lenL2 && !l1->next)
tempRet = l1;
int temp = l1->val + l2->val + sur;
l1->val = temp % 10;
sur = temp / 10;
l1 = l1->next;
l2 = l2->next;
}
while (l1) {
if (!l1->next)
tempRet = l1;
int temp = l1->val + sur;
l1->val = temp % 10;
sur = temp / 10;
l1 = l1->next;
}
if (sur == 1) {
ListNode *temp = new ListNode(1);
tempRet->next = temp;
}
return ret;
}
};