diff --git a/143_reorder_list/Makefile b/143_reorder_list/Makefile new file mode 100644 index 0000000..c6ca20b --- /dev/null +++ b/143_reorder_list/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test reorder_list.c diff --git a/143_reorder_list/reorder_list.c b/143_reorder_list/reorder_list.c new file mode 100644 index 0000000..a8d220a --- /dev/null +++ b/143_reorder_list/reorder_list.c @@ -0,0 +1,70 @@ +#include +#include + +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; +} diff --git a/147_insertion_sort_list/Makefile b/147_insertion_sort_list/Makefile new file mode 100644 index 0000000..576b707 --- /dev/null +++ b/147_insertion_sort_list/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test insert_sort_list.c diff --git a/147_insertion_sort_list/insert_sort_list.c b/147_insertion_sort_list/insert_sort_list.c new file mode 100644 index 0000000..bfae46c --- /dev/null +++ b/147_insertion_sort_list/insert_sort_list.c @@ -0,0 +1,61 @@ +#include +#include + +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; +}