-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd2nosreverse.cpp
62 lines (60 loc) · 1.02 KB
/
add2nosreverse.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
#include<stdlib.h>
#include<iostream>
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* insert(ListNode* ptr,int key)
{
ptr=new ListNode(key);
return ptr;
}
ListNode* add(ListNode* l1,ListNode* l2,int c)
{
int temp=0;
ListNode* carry;
if(l1&&l2)
{
temp=l1->val+l2->val+c;
c=temp/10;
carry = insert(carry,temp%10);
carry->next=add(l1->next,l2->next,c);
return carry;
}
else if(l1&&!l2)
{
temp=l1->val+c;
carry = insert(carry,temp%10);
c=temp/10;
carry->next=add(l1->next,l2,c);
return carry;
}
else if(!l1&&l2)
{
temp=l2->val+c;
carry=insert(carry,temp%10);
c=temp/10;
carry->next=add(l1,l2->next,c);
return carry;
}
else
{
if (c>0)
{
carry=insert(carry,c);
return carry;
}
else return NULL;
}
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* carry;
carry=add(l1,l2,0);
return carry;
}
int main(int argc, char const *argv[])
{
/* code */
return 0;
}