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: Leo Ma <[email protected]>
- Loading branch information
1 parent
343e2d8
commit 4802005
Showing
4 changed files
with
135 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 reorder_list.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,70 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
struct ListNode { | ||
int val; | ||
struct ListNode *next; | ||
}; | ||
|
||
static void reverse(struct ListNode *dummy) | ||
{ | ||
struct ListNode *prev = dummy->next; | ||
if (prev != NULL) { | ||
struct ListNode *p = prev->next; | ||
while (p != NULL) { | ||
prev->next = p->next; | ||
p->next = dummy->next; | ||
dummy->next = p; | ||
p = prev->next; | ||
} | ||
} | ||
} | ||
|
||
static void reorderList(struct ListNode *head) | ||
{ | ||
if (head == NULL) { | ||
return; | ||
} | ||
|
||
int count = 0; | ||
struct ListNode *p = head; | ||
struct ListNode *q = p; | ||
for (; p != NULL; p = p->next) { | ||
if ((++count & 0x1) == 0) { | ||
q = q->next; | ||
} | ||
} | ||
|
||
reverse(q); | ||
|
||
struct ListNode *r; | ||
for (p = head, r = q->next; r != NULL; p = r->next, r = q->next) { | ||
q->next = r->next; | ||
r->next = p->next; | ||
p->next = r; | ||
} | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int i, count = argc - 1; | ||
struct ListNode *head = NULL, *p, *prev; | ||
for (i = 0; i < count; i++) { | ||
p = malloc(sizeof(*p)); | ||
p->val = atoi(argv[i + 1]); | ||
p->next = NULL; | ||
if (head == NULL) { | ||
head = p; | ||
} else { | ||
prev->next = p; | ||
} | ||
prev = p; | ||
} | ||
|
||
reorderList(head); | ||
for (p = head; p != NULL; p = p->next) { | ||
printf("%d ", p->val); | ||
} | ||
printf("\n"); | ||
return 0; | ||
} |
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 insert_sort_list.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,61 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
struct ListNode { | ||
int val; | ||
struct ListNode *next; | ||
}; | ||
|
||
static struct ListNode *insertionSortList(struct ListNode *head) | ||
{ | ||
if (head == NULL) { | ||
return NULL; | ||
} | ||
|
||
if (head->next == NULL) { | ||
return head; | ||
} | ||
|
||
struct ListNode dummy; | ||
struct ListNode *p0, *p, *p1; | ||
dummy.next = head; | ||
|
||
for (p0 = head, p = head->next; p != NULL; p0 = p, p = p->next) { | ||
if (p->val < p0->val) { | ||
p0->next = p->next; | ||
for (p1 = &dummy; p1 != p0; p1 = p1->next) { | ||
if (p1->next->val >= p->val) { | ||
p->next = p1->next; | ||
p1->next = p; | ||
break; | ||
} | ||
} | ||
p = p0; | ||
} | ||
} | ||
|
||
return dummy.next; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
int i, count = argc - 1; | ||
struct ListNode *head = NULL, *p, *prev; | ||
for (i = 0; i < count; i++) { | ||
p = malloc(sizeof(*p)); | ||
p->val = atoi(argv[i + 1]); | ||
p->next = NULL; | ||
if (head == NULL) { | ||
head = p; | ||
} else { | ||
prev->next = p; | ||
} | ||
prev = p; | ||
} | ||
|
||
for (p = insertionSortList(head); p != NULL; p = p->next) { | ||
printf("%d ", p->val); | ||
} | ||
printf("\n"); | ||
return 0; | ||
} |