/
2-add-two-number.cpp
99 lines (79 loc) · 1.98 KB
/
2-add-two-number.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
void printList(ListNode *node){
while(node != NULL){
cout<<node->val<<" ";
node = node->next;
}
cout<<endl;
}
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *res, *curr;
ListNode begin(0);
res = &begin;
curr = &begin;
int ifCarry = 0;
while(l1 != NULL || l2 != NULL){
// when either of l1 or l2 don't come to end
if(l1 == NULL && l2 != NULL){
curr->val = l2->val;
}else if(l1 != NULL && l2 == NULL){
curr->val = l1->val;
}
// when both l1 and l2 don't come to end
if(l1 != NULL && l2 != NULL){
curr->val = l1->val + l2->val + ifCarry;
if(curr->val > 9){
curr->val -= 10;
ifCarry = 1;
}
}
l1 = l1->next;
l2 = l2->next;
ListNode* newNode = new ListNode(3);
// ListNode node(0);
curr->next = newNode;
curr = curr->next;
}
if(ifCarry){
ListNode* newNode = new ListNode(1);
curr->next = newNode;
curr = curr->next;
curr->next = NULL;
}else{
cout<<curr->val<<endl;
delete curr;
cout<<curr->val<<endl;
}
return res;
}
};
int main(void)
{
// Build two lists
ListNode a1(1);
ListNode a2(2);
ListNode a3(3);
a1.next = &a2;
a2.next = &a3;
a3.next = NULL;
ListNode b1(4);
ListNode b2(5);
ListNode b3(6);
b1.next = &b2;
b2.next = &b3;
b3.next = NULL;
ListNode* a = &a1;
ListNode* b = &b1;
// Add two lists
Solution solution;
ListNode* res = solution.addTwoNumbers(a,b);
printList(res);
}