Skip to content

Commit

Permalink
Reorder and insert list
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Ma <[email protected]>
  • Loading branch information
begeekmyfriend committed Oct 29, 2017
1 parent 343e2d8 commit 4802005
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 143_reorder_list/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test reorder_list.c
70 changes: 70 additions & 0 deletions 143_reorder_list/reorder_list.c
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;
}
2 changes: 2 additions & 0 deletions 147_insertion_sort_list/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test insert_sort_list.c
61 changes: 61 additions & 0 deletions 147_insertion_sort_list/insert_sort_list.c
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;
}

0 comments on commit 4802005

Please sign in to comment.