-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathN D05 - Harry Potter and the Goblet of Fire.c
152 lines (135 loc) · 2.93 KB
/
N D05 - Harry Potter and the Goblet of Fire.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
#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;
};
*/
int length(LinkedListNode* head)
{
int l=0;
LinkedListNode* temp=head;
while(temp)
{
temp=temp->next;
l++;
}
return l;
}
bool findIntersection(LinkedListNode* head1, LinkedListNode* head2) {
int l1=length(head1);
int l2=length(head2);
int d=0;
LinkedListNode* ptr1;
LinkedListNode* ptr2;
if(l1>l2)
{
d=l1-l2;
ptr1=head1;
ptr2=head2;
}
else
{
d=l2-l1;
ptr1=head2;
ptr2=head1;
}
while(d)
{
ptr1=ptr1->next;
if(ptr1==NULL)
{
return 0;
}
d--;
}
while(ptr1!=NULL &&ptr2!=NULL)
{
if(ptr1==ptr2)
{
return 1;
}
ptr1=ptr1->next;
ptr2=ptr2->next;
}
return 0;
}
int main()
{
FILE *f = stdout;
char *output_path = getenv("OUTPUT_PATH");
if (output_path) {
f = fopen(output_path, "w");
}
bool res;
int head1_size = 0;
LinkedListNode* head1 = NULL;
LinkedListNode* head1_tail = NULL;
scanf("%d", &head1_size);
for(int i = 0; i < head1_size; i++) {
int head1_item;
scanf("%d", &head1_item);
head1_tail = _insert_node_into_singlylinkedlist(head1, head1_tail, head1_item);
if(i == 0) {
head1 = head1_tail;
}
}
int head2_size = 0;
LinkedListNode* head2 = NULL;
LinkedListNode* head2_tail = NULL;
scanf("%d", &head2_size);
for(int i = 0; i < head2_size; i++) {
int head2_item;
scanf("%d", &head2_item);
head2_tail = _insert_node_into_singlylinkedlist(head2, head2_tail, head2_item);
if(i == 0) {
head2 = head2_tail;
}
}
int token;
scanf("%d", &token);
if (token != 0){
token--;
LinkedListNode* ptr = head1;
while(token--){
ptr = ptr->next;
}
if (head2 == NULL) head2 = ptr;
else head2_tail->next = ptr;
}
res = findIntersection(head1, head2);
fprintf(f, "%d\n", res);
fclose(f);
return 0;
}