-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path086-Partition List.c
45 lines (45 loc) · 1.25 KB
/
086-Partition List.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* partition(struct ListNode* head, int x) {
struct ListNode* i = NULL;
struct ListNode* j = head;
struct ListNode* prej;
struct ListNode* tmp;
while (j) {
if (j->val >= x) {
prej = j;
j = j->next;
} else {
if (i == NULL && j == head) {
// hasn't met the node greater than or equal to x
i = head;
j = j->next;
} else if (i == NULL) {
// has met the node greater than or equal to x
i = j;
j = j->next;
prej->next = j;
i->next = head;
head = i;
} else if (i->next == j) {
// hasn't met the node greater than or equal to x
i = j;
j = j->next;
} else {
// hasn met the node greater than or equal to x
tmp = j;
j = j->next;
prej->next = j;
tmp->next = i->next;
i->next = tmp;
i = i->next;
}
}
}
return head;
}