forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: begeekmyfriend <[email protected]>
- Loading branch information
1 parent
c56dc80
commit d93e8b1
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test reverse_nodes.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
struct ListNode { | ||
int val; | ||
struct ListNode *next; | ||
}; | ||
|
||
static struct ListNode* reverseKGroup(struct ListNode* head, int k) { | ||
if (head == NULL || k <= 1) { | ||
return head; | ||
} | ||
|
||
int first = 1; | ||
struct ListNode dummy; | ||
dummy.next = head; | ||
struct ListNode *dhead = &dummy; | ||
struct ListNode *prev = head; | ||
struct ListNode *p = head->next; | ||
struct ListNode *next = p == NULL ? NULL : p->next; | ||
while (p != NULL) { | ||
int i; | ||
struct ListNode *end = dhead->next; | ||
for (i = 0; end != NULL && i < k; i++) { | ||
end = end->next; | ||
} | ||
if (i < k) { | ||
break; | ||
} | ||
|
||
while (p != end) { | ||
p->next = dhead->next; | ||
dhead->next = p; | ||
prev->next = next; | ||
p = next; | ||
next = p == NULL ? NULL : p->next; | ||
} | ||
|
||
if (first) { | ||
first = 0; | ||
dummy.next = dhead->next; | ||
} | ||
|
||
dhead = prev; | ||
prev = p; | ||
p = p == NULL ? NULL : p->next; | ||
next = p == NULL ? NULL : p->next; | ||
} | ||
return dummy.next; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int i; | ||
struct ListNode *p, *prev, dummy, *list; | ||
|
||
dummy.next = NULL; | ||
prev = &dummy; | ||
for (i = 2; i < argc; i++) { | ||
p = malloc(sizeof(*p)); | ||
int n = atoi(argv[i]); | ||
printf("%d ", n); | ||
p->val = n; | ||
p->next = NULL; | ||
prev->next = p; | ||
prev = p; | ||
} | ||
putchar('\n'); | ||
|
||
list = reverseKGroup(dummy.next, atoi(argv[1])); | ||
for (p = list; p != NULL; p = p->next) { | ||
printf("%d ", p->val); | ||
} | ||
putchar('\n'); | ||
|
||
return 0; | ||
} |