-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathN D04 - Harry Potter and the Prisoner of Azkaban.c
165 lines (146 loc) · 3.76 KB
/
N D04 - Harry Potter and the Prisoner of Azkaban.c
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
154
155
156
157
158
159
160
161
162
163
164
165
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
typedef struct LinkedListNode LinkedListNode;
struct LinkedListNode {
int val;
LinkedListNode *next;
};
LinkedListNode* _insert_node_into_singlylinkedlist(LinkedListNode *head, LinkedListNode *tail, int val) {
if (head == NULL) {
head = (LinkedListNode *) (malloc(sizeof(LinkedListNode)));
head->val = val;
head->next = NULL;
tail = head;
}
else {
LinkedListNode *node = (LinkedListNode *) (malloc(sizeof(LinkedListNode)));
node->val = val;
node->next = NULL;
tail->next = node;
tail = tail->next;
}
return tail;
}
/*
* Complete the function below.
*/
/*
For your reference:
LinkedListNode {
int val;
LinkedListNode *next;
};
*/
LinkedListNode* addLists(LinkedListNode* list1, LinkedListNode* list2) {
int a[100], b[100];
int i = 0, j = 0;
struct LinkedListNode *temp = (struct LinkedListNode *)malloc(sizeof(struct LinkedListNode));
while (list1)
{
a[i++] = list1->val;
list1 = list1->next;
}
while (list2)
{
b[j++] = list2->val;
list2 = list2->next;
}
int sizea = i, sizeb = j;
if (i >= j) {
while (i && j)
{
a[--i] += b[--j];
}
for (int x = sizea - 1; x > 0; x--)
{
if (a[x] >= 10)
{
a[x] %= 10;
a[x - 1] += 1;
}
}
// for(int x=0;x<sizea;x++)
// printf("%d ",a[x]);
}
else
{
while (i && j)
{
b[--j] += a[--i];
}
for (int x = sizeb - 1; x > 0; x--)
{
if (b[x] >= 10)
{
b[x] %= 10;
b[x - 1] += 1;
}
}
// for(int x=0;x<sizeb;x++)
// printf("%d ",b[x]);
for (int y = 0; y < sizeb; y++)
a[y] = b[y];
}
int maxs = sizea >= sizeb ? sizea : sizeb;
// printf("%d ",maxs);
LinkedListNode *temp1, *temphead = NULL;
for (int co = maxs - 1; co >= 0; co--)
{
/*temp->val=a[co];
temp=temp->next;
if(temp==NULL)
break;*/
temp1 = ( LinkedListNode *)malloc(sizeof( LinkedListNode));
temp1->val = a[co];
temp1->next = temphead;
temphead = temp1;
//printf("%d",a[co]);
}
// temp->next=NULL;
return temphead;
}
int main()
{
FILE *f = stdout;
char *output_path = getenv("OUTPUT_PATH");
if (output_path) {
f = fopen(output_path, "w");
}
LinkedListNode* res;
int list1_size = 0;
LinkedListNode* list1 = NULL;
LinkedListNode* list1_tail = NULL;
scanf("%d\n", &list1_size);
for (int i = 0; i < list1_size; i++) {
int list1_item;
scanf("%d", &list1_item);
list1_tail = _insert_node_into_singlylinkedlist(list1, list1_tail, list1_item);
if (i == 0) {
list1 = list1_tail;
}
}
int list2_size = 0;
LinkedListNode* list2 = NULL;
LinkedListNode* list2_tail = NULL;
scanf("%d\n", &list2_size);
for (int i = 0; i < list2_size; i++) {
int list2_item;
scanf("%d", &list2_item);
list2_tail = _insert_node_into_singlylinkedlist(list2, list2_tail, list2_item);
if (i == 0) {
list2 = list2_tail;
}
}
res = addLists(list1, list2);
while (res != NULL) {
fprintf(f, "%d", res->val);
res = res->next;
}
fclose(f);
return 0;
}