-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_ll.c
More file actions
109 lines (99 loc) · 2.37 KB
/
Copy pathreverse_ll.c
File metadata and controls
109 lines (99 loc) · 2.37 KB
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
#include <stdio.h>
#include <stdlib.h>
typedef struct llnode node;
typedef struct llh head;
struct llh
{
struct llnode *first_node;
int length;
};
struct llnode
{
int val;
struct llnode *next;
};
node *create_node(int val)
{
node *newnode = (node *)malloc(sizeof(node));
newnode->val = val;
newnode->next = NULL;
return newnode;
}
void insert_tail(head *h, int val)
{
node *nn = create_node(val);
if (h->first_node == NULL)
{
h->first_node = nn;
h->length++;
return;
}
node *cur = h->first_node;
while (cur->next != NULL)
cur = cur->next;
cur->next = nn;
h->length++;
}
void print_ll(head *h)
{
node *cur = h->first_node;
while (cur != NULL)
{
printf("%d ", cur->val);
cur = cur->next;
}
}
node *reverse(node *h);
void reverse_ll(head *h, int k)
{
node *dummy = create_node(-1);
dummy->next = h->first_node;
node *prev_gp_end, *ed;
prev_gp_end = ed = dummy; // prev_gp_end is the left boundary
while (1)
{
for (int i = 0; i < k && ed; i++)
ed = ed->next; // ed is the original tail of the current group, which will become the first one after inverted, left boundary
if (!ed)
break;
node *ori_st = prev_gp_end->next; // original start of the current group will become the last one after inverted, which the right boundary
node *next_gp_st = ed->next; // right boundary
ed->next = NULL; // disconnect the current group from the rest of the list
prev_gp_end->next = reverse(prev_gp_end->next); // connecting the left boundary
ori_st->next = next_gp_st;
prev_gp_end = ed = ori_st;
}
h->first_node = dummy->next;
}
node *reverse(node *h)
{
node *cur = h;
node *pre = NULL;
while (cur)
{
node *next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
int main()
{
head *h = (head *)malloc(sizeof(head));
h->first_node = NULL;
h->length = 0;
insert_tail(h, 1);
insert_tail(h, 2);
insert_tail(h, 3);
insert_tail(h, 4);
insert_tail(h, 5);
insert_tail(h, 6);
insert_tail(h, 7);
insert_tail(h, 8);
insert_tail(h, 9);
insert_tail(h, 10);
reverse_ll(h, 4);
print_ll(h);
return 0;
}