-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCCT B1 - Harry Potter and the Deathly Hallows.c
94 lines (82 loc) · 1.81 KB
/
CCT B1 - Harry Potter and the Deathly Hallows.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
#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;
};
*/
void deleteNode(LinkedListNode* x) {
if (x->next==NULL)
{
return;
}
x->val=x->next->val;
LinkedListNode *temp;
temp = x->next;
x->next=x->next->next;
free(temp);
}
int main()
{
FILE *f = stdout;
char *output_path = getenv("OUTPUT_PATH");
if (output_path) {
f = fopen(output_path, "w");
}
LinkedListNode* res;
int x_size = 0;
LinkedListNode* x = NULL;
LinkedListNode* x_tail = NULL;
scanf("%d\n", &x_size);
for(int i = 0; i < x_size; i++) {
int x_item;
scanf("%d", &x_item);
x_tail = _insert_node_into_singlylinkedlist(x, x_tail, x_item);
if(i == 0) {
x = x_tail;
}
}
int p; scanf("%d", &p);
p--;
LinkedListNode* ptr = x;
while(p--) ptr = ptr->next;
deleteNode(ptr);
res = x;
while (res != NULL) {
fprintf(f, "%d\n", res->val);
res = res->next;
}
fclose(f);
return 0;
}