-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp086.c
39 lines (39 loc) · 1.03 KB
/
p086.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* partition(struct ListNode* head, int x) {
struct ListNode *prevtail = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode *tmphead = prevtail;
prevtail->next = head;
struct ListNode *pivottail = head;
while (pivottail != NULL && pivottail->val < x)
{
pivottail = pivottail->next;
prevtail = prevtail->next;
}
if (pivottail == NULL) return head;
struct ListNode *cur = pivottail->next;
struct ListNode *prev = pivottail;
while (cur != NULL)
{
if (cur->val < x)
{
prevtail->next = cur;
prev->next = cur->next;
cur->next = pivottail;
prevtail = cur;
cur = prev->next;
}
else
{
cur = cur->next;
prev = prev->next;
}
}
cur = tmphead->next;
return tmphead->next;
}