-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path86. Partition List.cpp
More file actions
52 lines (52 loc) · 1.26 KB
/
Copy path86. Partition List.cpp
File metadata and controls
52 lines (52 loc) · 1.26 KB
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode *left = NULL, *right = NULL, *cur = head, *res1 = NULL, *res2 = NULL;
while (cur != NULL)
{
if (cur->val < x)
{
if (left == NULL)
{
left = cur;
res1 = cur;
}
else
{
left->next = cur;
left = left->next;
}
}
else
{
if (right == NULL)
{
right = cur;
res2 = cur;
}
else
{
right->next = cur;
right = right->next;
}
}
cur = cur->next;
}
if (left == NULL || right == NULL)
return head;
else
{
right->next = NULL;
left->next = res2;
return res1;
}
}
};