-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode#2(Add Two numbers).cpp
More file actions
42 lines (41 loc) · 1.11 KB
/
Leetcode#2(Add Two numbers).cpp
File metadata and controls
42 lines (41 loc) · 1.11 KB
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
/**
* 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* new_var = new ListNode();
ListNode* temp = new_var;
int carry_sum = 0;
while(l1!=0 || l2!=0 || carry_sum==1)
{
int sum = 0;
if(l1!=0){
sum += l1->val;
l1 = l1->next;
}
if(l2!=0){
sum += l2->val;
l2 = l2->next;
}
//cout<<sum<<endl;
sum += carry_sum;
//cout<<sum<<endl;
carry_sum = sum/10;
sum = sum%10;
//cout<<sum<<endl;
ListNode* new_node = new ListNode(sum);
temp->next = new_node;
temp = temp->next;
//cout<<temp->val<<endl;
}
return new_var->next;
}
};