-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.cpp
More file actions
37 lines (37 loc) · 907 Bytes
/
2.cpp
File metadata and controls
37 lines (37 loc) · 907 Bytes
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int carry = 0;
if(l1==NULL) return l2;
if(l2==NULL) return l1;
ListNode* h = l1;
ListNode* pre = l1;
while(l1!=NULL)
{
int l2val = l2?l2->val:0;
l1->val += l2val+carry;
carry = l1->val/10;
l1->val= l1->val%10;
pre = l1;
l1 = l1->next;
l2 = l2?l2->next:NULL;
if(l1 == NULL) {
pre->next = l2;
l2 = NULL;
l1 = pre->next;
}
}
if(carry!=0){
pre->next = new ListNode(1);
}
return h;
}
};