-
Notifications
You must be signed in to change notification settings - Fork 2
/
2.cpp
133 lines (123 loc) · 3.54 KB
/
2.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
// Author : Accagain
// Date : 17/2/14
// Email : [email protected]
/************************************************************************
*
* You are given two non-empty linked lists representing two non-negative integers.
*
* The digits are stored in reverse order and each of their nodes contain a single digit.
*
* Add the two numbers and return it as a linked list.
*
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
*
*
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4
*
* Output: 7 -> 0 -> 8
*
*************************************************************************/
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int gain = 0;
ListNode * ans = (ListNode *)malloc(sizeof(ListNode));
ListNode * head = ans;
while((l1 != NULL) && (l2 != NULL))
{
ListNode * now = (ListNode * )malloc(sizeof(ListNode));
now->val = ((l1->val + l2->val + gain) % 10);
now->next = NULL;
gain = (l1->val + l2->val + gain) / 10;
ans->next = now;
ans = ans->next;
l1 = l1->next;
l2 = l2->next;
}
while(l1 != NULL)
{
ListNode * now = (ListNode *)malloc(sizeof(ListNode));
now->val = ((l1->val + gain) % 10);
now->next = NULL;
gain = (l1->val + gain) / 10;
ans->next = now;
ans = ans->next;
l1 = l1->next;
}
while(l2 != NULL)
{
ListNode * now = (ListNode *)malloc(sizeof(ListNode));
now->val = ((l2->val + gain) % 10);
now->next = NULL;
gain = (l2->val + gain) / 10;
ans->next = now;
ans = ans->next;
l2 = l2->next;
}
while(gain)
{
ListNode * now = (ListNode *)malloc(sizeof(ListNode));
now->val = (gain % 10);
now->next = NULL;
gain = gain / 10;
ans->next = now;
ans = ans->next;
}
return head->next;
}
};
int main()
{
int testN;
scanf("%d", &testN);
for(int i=1; i<=testN; i++)
{
int n;
scanf("%d", &n);
ListNode * l1 = (ListNode *)malloc(sizeof(ListNode));
l1->next = NULL;
ListNode * cur = l1;
for(int j=1; j<=n; j++)
{
int temp;
scanf("%d", &temp);
//printf("%d", temp);
ListNode * now = (ListNode * ) malloc(sizeof(ListNode));
now->next = NULL;
now->val = temp;
cur->next = now;
cur = cur->next;
}
ListNode * l2 = (ListNode *)malloc(sizeof(ListNode));
l2->next = NULL;
cur = l2;
scanf("%d", &n);
for(int j=1; j<=n; j++)
{
int temp;
scanf("%d", &temp);
ListNode * now = (ListNode * ) malloc(sizeof(ListNode));
now->next = NULL;
now->val = temp;
cur->next = now;
cur = cur->next;
}
//printf("%d ** %d\n", l1->next->val, l2->next->val);
Solution* test = new Solution();
ListNode * ans = test->addTwoNumbers(l1->next, l2->next);
while(ans != NULL)
{
printf("%d ", ans->val);
ans = ans->next;
}
}
}