- Notifications
You must be signed in to change notification settings - Fork 117
/
Copy path86.c
74 lines (62 loc) · 1.35 KB
/
86.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<stdio.h>
#include<stdlib.h>
structListNode {
intval;
structListNode*next;
};
structListNode*partition(structListNode*head, intx) {
if (head==NULL) returnNULL;
structListNode*left, *right;
structListNode*lp, *rp;
structListNode*p;
left= (structListNode*)calloc(1, sizeof(structListNode));
right= (structListNode*)calloc(1, sizeof(structListNode));
lp=left;
rp=right;
p=head;
while (p) {
if (p->val<x) {
lp->next=p;
lp=lp->next;
}
else {
rp->next=p;
rp=rp->next;
}
p=p->next;
}
lp->next=right->next;
rp->next=NULL;
head=left->next;
free(left); free(right);
returnhead;
}
intmain() {
structListNode*head= (structListNode*)calloc(6, sizeof(structListNode));
structListNode*p=head;
p->val=1;
p->next=p+1;
p=p->next;
p->val=4;
p->next=p+1;
p=p->next;
p->val=3;
p->next=p+1;
p=p->next;
p->val=2;
p->next=p+1;
p=p->next;
p->val=5;
p->next=p+1;
p=p->next;
p->val=2;
p->next=NULL;
intx=3;
p=partition(head, x);
while (p) {
printf("%d ", p->val);
p=p->next;
}
printf("\n");
return0;
}