forked from amritanand-py/cps02
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN D02 - Harry Potter and the Philosopher's Stone.c
221 lines (187 loc) · 5.08 KB
/
N D02 - Harry Potter and the Philosopher's Stone.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#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 {
char val;
LinkedListNode *next;
};
LinkedListNode* _insert_node_into_singlylinkedlist(LinkedListNode *head, LinkedListNode *tail, char 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 {
char val;
LinkedListNode *next;
};
*/
void reverse(LinkedListNode**);
bool compareLists(LinkedListNode*,LinkedListNode*);
// Function to check if given linked
// list is palindrome or not
// bool isPalindrome(struct Node* head)
bool isPalindrome(LinkedListNode* head)
{
LinkedListNode *slow_ptr = head,
*fast_ptr = head;
LinkedListNode *second_half,
*prev_of_slow_ptr = head;
// To handle odd size list
LinkedListNode* midnode = NULL;
// Initialize result
bool res = true;
if (head != NULL &&
head->next != NULL)
{
// Get the middle of the list.
// Move slow_ptr by 1 and
// fast_ptrr by 2, slow_ptr
// will have the middle node
while (fast_ptr != NULL &&
fast_ptr->next != NULL)
{
fast_ptr = fast_ptr->next->next;
// We need previous of the slow_ptr for
// linked lists with odd elements
prev_of_slow_ptr = slow_ptr;
slow_ptr = slow_ptr->next;
}
/* fast_ptr would become NULL when there
are even elements in list. And not NULL
for odd elements. We need to skip the
middle node for odd case and store it
somewhere so that we can restore the
original list*/
if (fast_ptr != NULL)
{
midnode = slow_ptr;
slow_ptr = slow_ptr->next;
}
// Now reverse the second half and
// compare it with first half
second_half = slow_ptr;
// NULL terminate first half
prev_of_slow_ptr->next = NULL;
// Reverse the second half
reverse(&second_half);
// Compare
res = compareLists(head, second_half);
// Construct the original list back
// Reverse the second half again
reverse(&second_half);
// If there was a mid node (odd size
// case) which was not part of either
// first half or second half.
if (midnode != NULL)
{
prev_of_slow_ptr->next = midnode;
midnode->next = second_half;
}
else
prev_of_slow_ptr->next = second_half;
}
return res;
}
// Function to reverse the linked list
// Note that this function may change
// the head
void reverse(LinkedListNode** head_ref)
{
LinkedListNode* prev = NULL;
LinkedListNode *current = *head_ref;
LinkedListNode *next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
// Function to check if two input
// lists have same data
bool compareLists(LinkedListNode* head1,LinkedListNode* head2)
{
LinkedListNode* temp1 = head1;
LinkedListNode* temp2 = head2;
while (temp1 && temp2)
{
if (temp1->val == temp2->val)
{
temp1 = temp1->next;
temp2 = temp2->next;
}
else
return 0;
}
// Both are empty return 1
if (temp1 == NULL && temp2 == NULL)
return 1;
// Will reach here when one is NULL
// and other is not
return 0;
}
// Push a node to linked list.
// Note that this function
// changes the head
// void push(LinkedListNode** head_ref,
// char new_data)
// {
// // allocate node
// LinkedListNode* new_node =
// (struct Node*)malloc(sizeof(struct Node));
// // Put in the data
// new_node->data = new_data;
// // Link the old list off the new node
// new_node->next = (*head_ref);
// // Move the head to point to the new node
// (*head_ref) = new_node;
// }
int main()
{
FILE *f = stdout;
char *output_path = getenv("OUTPUT_PATH");
if (output_path) {
f = fopen(output_path, "w");
}
bool res;
int head_size = 0;
LinkedListNode* head = NULL;
LinkedListNode* head_tail = NULL;
scanf("%d", &head_size);
char s[10005];
scanf(" %s", s);
for(int i = 0; i < head_size; i++) {
char head_item = s[i];
head_tail = _insert_node_into_singlylinkedlist(head, head_tail, head_item);
if(i == 0) {
head = head_tail;
}
}
res = isPalindrome(head);
fprintf(f, "%d\n", res);
fclose(f);
return 0;
}