-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathadd_two_numbers.cpp
153 lines (144 loc) · 3.9 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution
{
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
ListNode *ansHead = new ListNode((l1->val + l2->val) % 10);
ListNode *ans = ansHead;
int carry = (l1->val + l2->val) / 10;
l1 = l1->next;
l2 = l2->next;
while (l1 != NULL && l2 != NULL)
{
ListNode *temp = new ListNode((l1->val + l2->val + carry) % 10);
carry = (l1->val + l2->val + carry) / 10;
l1 = l1->next;
l2 = l2->next;
ans->next = temp;
ans = temp;
}
while (l1 != NULL)
{
ListNode *temp = new ListNode((l1->val + carry) % 10);
carry = (l1->val + carry) / 10;
l1 = l1->next;
ans->next = temp;
ans = temp;
}
while (l2 != NULL)
{
ListNode *temp = new ListNode((l2->val + carry) % 10);
carry = (l2->val + carry) / 10;
l2 = l2->next;
ans->next = temp;
ans = temp;
}
if (carry > 0)
{
ListNode *temp = new ListNode(carry);
ans->next = temp;
ans = temp;
}
return ansHead;
}
ListNode *solve(ListNode *&head1, ListNode *&head2)
{
if (head1 == NULL)
{
return head2;
}
if (head2 == NULL)
{
return head1;
}
// linked list which contains the final answer
ListNode *ansHead = NULL;
ListNode *ansTail = NULL;
int carry = 0;
while (head1 != NULL && head2 != NULL)
{
int sum = head1->val + head2->val + carry;
int digit = sum % 10;
carry = sum / 10;
ListNode *newNode = new ListNode(digit);
if (ansHead == NULL)
{
ansHead = newNode;
ansTail = newNode;
}
else
{
ansTail->next = newNode;
ansTail = newNode;
}
head1 = head1->next;
head2 = head2->next;
}
// head1 linked list pending to be solved
while (head1 != NULL)
{
int sum = carry + head1->val;
int digit = sum % 10;
carry = sum / 10;
ListNode *newNode = new ListNode(digit);
if (ansHead == NULL)
{
ansHead = newNode;
ansTail = newNode;
}
else
{
ansTail->next = newNode;
ansTail = newNode;
}
head1 = head1->next;
}
// head2 linked list pending to be solved
while (head2 != NULL)
{
int sum = carry + head2->val;
int digit = sum % 10;
carry = sum / 10;
ListNode *newNode = new ListNode(digit);
if (ansHead == NULL)
{
ansHead = newNode;
ansTail = newNode;
}
else
{
ansTail->next = newNode;
ansTail = newNode;
}
head2 = head2->next;
}
while (carry != 0)
{
int sum = carry;
int digit = sum % 10;
carry = sum / 10;
ListNode *newNode = new ListNode(digit);
if (ansHead == NULL)
{
ansHead = newNode;
ansTail = newNode;
}
else
{
ansTail->next = newNode;
ansTail = newNode;
}
}
return ansHead;
}
};