-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-two-numbers.cpp
82 lines (75 loc) · 1.98 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
// ???????????????O(1)????????????????????
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* head=new ListNode(-1);
auto cur=l1;
head->next=cur;
int carry=0;
while(l1 || l2 || carry!=0){
if(l1){
carry+=l1->val;
l1=l1->next;
}
if(l2){
carry+=l2->val;
l2=l2->next;
}
cur->val=carry%10;
carry/=10;
if(l1) cur->next=l1;
else if(l2) cur->next=l2;
else if(carry) cur->next=new ListNode(carry);
cur=cur->next;
}
return head->next;
}
ListNode* addTwoNumbers1(ListNode* l1, ListNode* l2) {
ListNode *newhead=new ListNode(INT_MIN);
auto cur=newhead;
int carry=0;
while(l1 && l2){
int t=l1->val+l2->val+carry;
cur->next=new ListNode(t%10);
carry=t/10;
l1=l1->next;
l2=l2->next;
cur=cur->next;
}
while(l1){
int t=l1->val+carry;
cur->next=new ListNode(t%10);
carry=t/10;
l1=l1->next;
cur=cur->next;
}
while(l2){
int t=l2->val+carry;
cur->next=new ListNode(t%10);
carry=t/10;
l2=l2->next;
cur=cur->next;
}
while(carry){
cur->next=new ListNode(carry%10);
carry=carry/10;
}
return newhead->next;
}
ListNode* reverse(ListNode* root){
if(!root || !root->next) return root;
auto t=root->next;
auto res=reverse(t);
t->next=root;
root->next=nullptr;
return res;
}
};