-
Notifications
You must be signed in to change notification settings - Fork 0
/
25.reverse-nodes-in-k-group.c
153 lines (145 loc) · 3.57 KB
/
25.reverse-nodes-in-k-group.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
/*
* @lc app=leetcode.cn id=25 lang=c
*
* [25] Reverse Nodes in k-Group
*
* https://leetcode.cn/problems/reverse-nodes-in-k-group/description/
*
* algorithms
* Hard (68.30%)
* Likes: 2374
* Dislikes: 0
* Total Accepted: 641.4K
* Total Submissions: 939.2K
* Testcase Example: '[1,2,3,4,5]\n2'
*
* Given the head of a linked list, reverse the nodes of the list k at a time,
* and return the modified list.
*
* k is a positive integer and is less than or equal to the length of the
* linked list. If the number of nodes is not a multiple of k then left-out
* nodes, in the end, should remain as it is.
*
* You may not alter the values in the list's nodes, only nodes themselves may
* be changed.
*
*
* Example 1:
*
*
* Input: head = [1,2,3,4,5], k = 2
* Output: [2,1,4,3,5]
*
*
* Example 2:
*
*
* Input: head = [1,2,3,4,5], k = 3
* Output: [3,2,1,4,5]
*
*
*
* Constraints:
*
*
* The number of nodes in the list is n.
* 1 <= k <= n <= 5000
* 0 <= Node.val <= 1000
*
*
*
* Follow-up: Can you solve the problem in O(1) extra memory space?
*
*/
#include "include/type.h"
// @lc code=start
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *reverseKGroup(struct ListNode *head, int k)
{
if (head->next == NULL || k == 1)
{
return head;
}
struct ListNode *new_head = NULL;
// last-group-last-node and next-group-first-node and current-node and current-head
struct ListNode *l_g_l_node = NULL, *n_g_f_node = head, *c_node = head, *c_head, *c_c_node;
// current-group-size;
int c_g_s = 1;
while (1)
{
if (c_g_s == k)
{
n_g_f_node = c_node->next;
// reverse current group
if (l_g_l_node == NULL)
{
c_head = head;
}
else
{
c_head = l_g_l_node->next;
}
if (new_head == NULL)
{
new_head = c_node;
}
else
{
l_g_l_node->next = c_node;
}
c_c_node = c_node;
for (int i = k - 1; i > 0; i--)
{
c_node = c_head;
for (int j = 1; j < i; j++)
{
c_node = c_node->next;
}
c_c_node->next = c_node;
c_c_node = c_c_node->next;
}
l_g_l_node = c_c_node;
l_g_l_node->next = n_g_f_node;
c_node = n_g_f_node;
if (c_node == NULL)
{
break;
}
c_g_s = 1;
}
else
{
c_node = c_node->next;
if (c_node == NULL)
{
break;
}
c_g_s++;
}
}
return new_head;
}
// @lc code=end
#include <stdlib.h>
int main(int argc, char const *argv[])
{
struct ListNode *head = (struct ListNode *)malloc(sizeof(struct ListNode));
head->val = 1;
head->next = (struct ListNode *)malloc(sizeof(struct ListNode));
head->next->val = 2;
head->next->next = (struct ListNode *)malloc(sizeof(struct ListNode));
head->next->next->val = 3;
head->next->next->next = (struct ListNode *)malloc(sizeof(struct ListNode));
head->next->next->next->val = 4;
head->next->next->next->next = (struct ListNode *)malloc(sizeof(struct ListNode));
head->next->next->next->next->val = 5;
head->next->next->next->next->next = NULL;
reverseKGroup(head, 2);
return 0;
}