-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp002.cpp
46 lines (46 loc) · 1003 Bytes
/
p002.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
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *head = l1, *node = head, *l2head = l2;
int add = 0;
while (true)
{
int d1 = 0;
int d2 = 0;
if (l1 != NULL) d1 = l1->val;
if (l2 != NULL) d2 = l2->val;
node->val = d1+d2+add;
if (node->val >= 10)
{
node->val -= 10;
add = 1;
}
else
{
add = 0;
}
if (l1 != NULL) l1 = l1->next;
if (l2 != NULL) l2 = l2->next;
if (l1 == NULL && l2 == NULL)
{
if (add != 0)
{
if (node->next == NULL)
{
node->next = l2head;
}
node->next->val = add;
node->next->next = NULL;
}
else
{
node->next = NULL;
}
return head;
}
if (node->next == NULL) node->next = l2head;
node = node->next;
}
return head;
}
};