forked from Tushargupta9800/CSES-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2. Add Two Numbers.cpp
More file actions
55 lines (50 loc) · 1.36 KB
/
2. Add Two Numbers.cpp
File metadata and controls
55 lines (50 loc) · 1.36 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
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* 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:
char to_char(int n){
return (char)(n + '0');
}
int to_in(char c){
return (int)(c - '0');
}
pair<int, char> sum(char a, char b, int rem){
int sum = to_in(a) + to_in(b) + rem;
rem = sum/10;
sum = sum%10;
return {rem, sum};
}
ListNode* addTwoNumbers(ListNode* head1, ListNode* head2) {
int carry=0;
ListNode* head3 =new ListNode ((head1->val + head2->val + carry)%10);
carry=(head1->val + head2->val + carry)/10;
ListNode* t=head3;
head1=head1->next;
head2=head2->next;
while(head1!=NULL || head2!=NULL || carry!=0)
{
int sum=carry;
if(head1!=0){
sum=sum+head1->val;
head1=head1->next;
}
if(head2!=0){
sum=sum+head2->val;
head2=head2->next;
}
carry=sum/10;
ListNode* n=new ListNode(sum%10);
t->next=n;
t=n;
}
return head3;
}
};