Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 86ff32f

Browse files
committed
"Reverse Nodes in k-Group"
1 parent 2d4d173 commit 86ff32f

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
275275
| 28 | [Implement strStr()] | [C](src/28.c) |
276276
| 27 | [Remove Element] | [C](src/27.c) |
277277
| 26 | [Remove Duplicates from Sorted Array] | [C](src/26.c) |
278-
| 25 | [Reverse Nodes in k-Group] | |
278+
| 25 | [Reverse Nodes in k-Group] | [C](src/25.c) |
279279
| 24 | [Swap Nodes in Pairs] | [C](src/24.c) |
280280
| 23 | [Merge k Sorted Lists] | [C](src/23.c) |
281281
| 22 | [Generate Parentheses] | [C++](src/22.cpp) |

‎src/25.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
5+
struct ListNode {
6+
int val;
7+
struct ListNode *next;
8+
};
9+
10+
struct ListNode* createNode(int data) {
11+
struct ListNode *new_node = (struct ListNode *)malloc(sizeof(struct ListNode));
12+
new_node->val = data;
13+
new_node->next = NULL;
14+
return new_node;
15+
}
16+
17+
struct ListNode* reverseKGroup(struct ListNode* head, int k) {
18+
if (k <= 1 || head == NULL) return head;
19+
20+
struct ListNode *leap = head, *next_leap = NULL;
21+
struct ListNode *milestone = NULL, *next_milestone = NULL;
22+
struct ListNode *new_head = NULL;
23+
bool first = true;
24+
int len = 0;
25+
while (leap) {
26+
leap = leap->next;
27+
len++;
28+
}
29+
30+
if (k > len) return head;
31+
32+
leap = head;
33+
while (leap) {
34+
int step = k - 1;
35+
milestone = leap;
36+
while (milestone && step) {
37+
milestone = milestone->next;
38+
step--;
39+
}
40+
41+
if (milestone == NULL) {
42+
break;
43+
}
44+
45+
step = k - 1;
46+
next_milestone = milestone->next;
47+
while (next_milestone && step) {
48+
next_milestone = next_milestone->next;
49+
step--;
50+
}
51+
52+
if (next_milestone == NULL) {
53+
next_milestone = (milestone == NULL) ? NULL : milestone->next;
54+
}
55+
56+
next_leap = milestone->next;
57+
milestone->next = NULL;
58+
59+
/* reverse group */
60+
struct ListNode *p = leap, *q = NULL, *t = NULL;
61+
while (p) {
62+
t = p->next;
63+
p->next = q;
64+
q = p;
65+
p = t;
66+
}
67+
leap->next = next_milestone; /* group tail to next milestone */
68+
69+
if (first) {
70+
new_head = milestone;
71+
first = false;
72+
}
73+
74+
leap = next_leap;
75+
}
76+
77+
return new_head;
78+
}
79+
80+
int main() {
81+
int n = 5;
82+
int k = 3;
83+
84+
struct ListNode *head = createNode(1);
85+
struct ListNode *p = head;
86+
87+
int i;
88+
for (i = 2; i <= n; i++) {
89+
p->next = createNode(i);
90+
p = p->next;
91+
}
92+
93+
p = head;
94+
while (p) {
95+
printf("%d->", p->val);
96+
p = p->next;
97+
}
98+
printf("NIL\n");
99+
100+
struct ListNode *new_head = reverseKGroup(head, k);
101+
102+
p = new_head;
103+
while (p) {
104+
printf("%d->", p->val);
105+
p = p->next;
106+
}
107+
printf("NIL\n");
108+
109+
return 0;
110+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /