File tree Expand file tree Collapse file tree 4 files changed +135
-0
lines changed Expand file tree Collapse file tree 4 files changed +135
-0
lines changed Original file line number Diff line number Diff line change
1
+ all :
2
+ gcc -O2 -o test reorder_list.c
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ all :
2
+ gcc -O2 -o test insert_sort_list.c
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments