-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddTwoNumbers.cpp
52 lines (52 loc) · 1.24 KB
/
AddTwoNumbers.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
/**
* 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) {
ListNode* h1=l1;
ListNode* h2=l2;
int res=0;
while(h1!=NULL and h2!=NULL){
res+=h1->val+h2->val;
int n=res%10;
res/=10;
h1->val=n;
h1=h1->next;
h2=h2->next;
}
if(h1!=NULL){
while(res!=0 and h1!=NULL){
res+=h1->val;
int n=res%10;
res/=10;
h1->val=n;
h1=h1->next;
}
}
if(h2!=NULL){
ListNode* h4=h2;
while(res!=0 and h2!=NULL){
res+=h2->val;
int n=res%10;
res/=10;
h2->val=n;
h2=h2->next;
}
ListNode* h3=l1;
while(h3->next!=NULL) h3=h3->next;
h3->next=h4;
}
if(res!=0){
ListNode* h3=l1;
while(h3->next!=NULL) h3=h3->next;
h3->next=new ListNode(res);
}
return l1;
}
};