Skip to content

Commit 4802005

Browse files
Reorder and insert list
Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
1 parent 343e2d8 commit 4802005

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

143_reorder_list/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test reorder_list.c

143_reorder_list/reorder_list.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct ListNode {
5+
int val;
6+
struct ListNode *next;
7+
};
8+
9+
static void reverse(struct ListNode *dummy)
10+
{
11+
struct ListNode *prev = dummy->next;
12+
if (prev != NULL) {
13+
struct ListNode *p = prev->next;
14+
while (p != NULL) {
15+
prev->next = p->next;
16+
p->next = dummy->next;
17+
dummy->next = p;
18+
p = prev->next;
19+
}
20+
}
21+
}
22+
23+
static void reorderList(struct ListNode *head)
24+
{
25+
if (head == NULL) {
26+
return;
27+
}
28+
29+
int count = 0;
30+
struct ListNode *p = head;
31+
struct ListNode *q = p;
32+
for (; p != NULL; p = p->next) {
33+
if ((++count & 0x1) == 0) {
34+
q = q->next;
35+
}
36+
}
37+
38+
reverse(q);
39+
40+
struct ListNode *r;
41+
for (p = head, r = q->next; r != NULL; p = r->next, r = q->next) {
42+
q->next = r->next;
43+
r->next = p->next;
44+
p->next = r;
45+
}
46+
}
47+
48+
int main(int argc, char **argv)
49+
{
50+
int i, count = argc - 1;
51+
struct ListNode *head = NULL, *p, *prev;
52+
for (i = 0; i < count; i++) {
53+
p = malloc(sizeof(*p));
54+
p->val = atoi(argv[i + 1]);
55+
p->next = NULL;
56+
if (head == NULL) {
57+
head = p;
58+
} else {
59+
prev->next = p;
60+
}
61+
prev = p;
62+
}
63+
64+
reorderList(head);
65+
for (p = head; p != NULL; p = p->next) {
66+
printf("%d ", p->val);
67+
}
68+
printf("\n");
69+
return 0;
70+
}

147_insertion_sort_list/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test insert_sort_list.c
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct ListNode {
5+
int val;
6+
struct ListNode *next;
7+
};
8+
9+
static struct ListNode *insertionSortList(struct ListNode *head)
10+
{
11+
if (head == NULL) {
12+
return NULL;
13+
}
14+
15+
if (head->next == NULL) {
16+
return head;
17+
}
18+
19+
struct ListNode dummy;
20+
struct ListNode *p0, *p, *p1;
21+
dummy.next = head;
22+
23+
for (p0 = head, p = head->next; p != NULL; p0 = p, p = p->next) {
24+
if (p->val < p0->val) {
25+
p0->next = p->next;
26+
for (p1 = &dummy; p1 != p0; p1 = p1->next) {
27+
if (p1->next->val >= p->val) {
28+
p->next = p1->next;
29+
p1->next = p;
30+
break;
31+
}
32+
}
33+
p = p0;
34+
}
35+
}
36+
37+
return dummy.next;
38+
}
39+
40+
int main(int argc, char **argv)
41+
{
42+
int i, count = argc - 1;
43+
struct ListNode *head = NULL, *p, *prev;
44+
for (i = 0; i < count; i++) {
45+
p = malloc(sizeof(*p));
46+
p->val = atoi(argv[i + 1]);
47+
p->next = NULL;
48+
if (head == NULL) {
49+
head = p;
50+
} else {
51+
prev->next = p;
52+
}
53+
prev = p;
54+
}
55+
56+
for (p = insertionSortList(head); p != NULL; p = p->next) {
57+
printf("%d ", p->val);
58+
}
59+
printf("\n");
60+
return 0;
61+
}

0 commit comments

Comments
 (0)