-
Notifications
You must be signed in to change notification settings - Fork 1
/
reverse_list.c
111 lines (98 loc) · 2.76 KB
/
reverse_list.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
/* Meituan 2014 Campus Hiring
* Sep 10, 2013
*
* Problem Description:
* k链表翻转。给出一个链表和一个数k, 比如链表1->2->3->4->5->6, k=2,
* 则翻转后2->1->4->3->6->5, 若k=3, 翻转后3->2->1->6->5->4, 若k=4,
* 翻转后4->3->2->1->5->6, 用程序实现。
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 12
struct Node {
int data;
struct Node *next;
};
void llist_destroy(struct Node *header) {
struct Node *tmp, *curr = header;
while(curr != NULL) {
tmp = curr->next;
free(curr);
curr = tmp;
}
}
struct Node *llist_init(void) {
int i = 1;
struct Node *curr, *header = NULL, *prev=NULL;
while(i <= N) {
curr = malloc(sizeof(struct Node));
if(!curr) {
llist_destroy(header); /* clean up */
return NULL;
}
curr->data = i++;
curr->next = NULL;
if(!header)
header = curr;
else
prev->next = curr;
prev = curr;
}
return header;
}
/* Reverse the region between first and last */
static void inner_reverse(struct Node *first, struct Node *last) {
struct Node *next_, *curr = first, *prev = NULL, *stop = last->next;
while (curr != stop) {
next_ = curr->next;
curr->next = prev;
prev = curr;
curr = next_;
}
}
struct Node * llist_reverse_by_k(struct Node *header, int k) {
if(!header || k < 2) return header; /* Nothing to do */
struct Node *curr = header, *prev = NULL;
struct Node *first, *last;
do {
first = curr;
for(int i=1; i < k; i++) { /* to find the last node of every k nodes */
curr = curr->next;
if(!curr) { /* not enough nodes, return early */
if(prev)
prev->next = first;
return header;
}
}
last = curr;
curr = curr->next; /* to preserve the first node for next iteration */
inner_reverse(first, last);
if(!prev) {
header = last;
} else {
prev->next = last;
}
prev = first;
} while (curr);
return header;
}
int main(int argc, char *argv[]) {
struct Node *curr, *header;
int k;
if(argc != 2 || strcmp(argv[1], "--help") == 0) {
fprintf(stderr, "%s k\n", argv[0]);
exit(EXIT_FAILURE);
}
k = atoi(argv[1]);
header = llist_init(); /* Initialize the single linked list */
for(curr = header; curr != NULL; curr = curr->next)
printf("%d ", curr->data);
printf("\n");
header = llist_reverse_by_k(header, k);
for(curr = header; curr != NULL; curr = curr->next)
printf("%d ", curr->data);
printf("\n");
llist_destroy(header);
return 0;
}