-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-two-numbers.cpp
63 lines (61 loc) · 1.77 KB
/
add-two-numbers.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// @Title: 两数相加 (Add Two Numbers)
// @Author: [email protected]
// @Date: 2020-06-18 22:14:04
// @Runtime: 40 ms
// @Memory: 9.3 MB
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
static int x = [](){ std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); return NULL; }();
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *cl1=l1;
ListNode *cl2=l2;
ListNode *p = new ListNode((cl1->val+cl2->val)%10);
ListNode *start = p;
int jinwei=(cl1->val+cl2->val >= 10? 1:0);
while(cl1->next!=NULL || cl2->next!= NULL)
{
if(cl1->next==NULL)
{
cl2 = cl2->next;
int sum = cl2->val + jinwei;
ListNode *q = new ListNode(sum%10);
p->next = q;
p = q;
jinwei=(sum >= 10? 1:0);
continue;
}
if(cl2->next==NULL)
{
cl1 = cl1->next;
int sum = cl1->val + jinwei;
ListNode *q = new ListNode(sum%10);
p->next = q;
p = q;
jinwei=(sum >= 10? 1:0);
continue;
}
cl1 = cl1->next;
cl2 = cl2->next;
int sum = cl1->val + cl2->val + jinwei;
ListNode *q = new ListNode(sum%10);
p->next = q;
p = q;
jinwei=(sum >= 10? 1:0);
}
if(jinwei != 0)
{
ListNode *q = new ListNode(jinwei);
p->next = q;
p = q;
}
return start;
}
};