-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd_Two_Numbers.cpp
68 lines (62 loc) · 1.23 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
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
int AddNum(int a,int b,int &carry)
{
int sum = a + b + carry;
if (sum >= 10)
{
carry = 1;
return sum - 10;
}
else
{
carry = 0;
return sum;
}
}
ListNode *AddList(ListNode *a, ListNode *b,int &carry)
{
if (a == NULL && b == NULL)
if (carry)
return new ListNode(1);
else
return NULL;
if (a == NULL)
{
ListNode *tmp = new ListNode(AddNum(0,b->val,carry));
tmp->next = AddList(NULL,b->next,carry);
return tmp;
}
if (b == NULL)
{
ListNode *tmp = new ListNode(AddNum(a->val,0,carry));
tmp->next = AddList(a->next,NULL,carry);
return tmp;
}
ListNode *tmp = new ListNode(AddNum(a->val,b->val,carry));
tmp->next = AddList(a->next,b->next,carry);
return tmp;
}
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int carry = 0;
return AddList(l1,l2,carry);
}
int main()
{
ListNode *a = new ListNode(4);
ListNode *b = new ListNode(7);
ListNode *sum = addTwoNumbers(a,b);
while(sum!=NULL)
{
cout << sum->val << "-";
sum = sum->next;
}
return 0;
}