forked from Rahul-skush/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path86. Partition List
40 lines (35 loc) · 981 Bytes
/
86. Partition List
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
class Solution {
public:
ListNode* helper(ListNode* head1, int x)
{
ListNode* head = head1;
if(!head) return head;
ListNode *left=NULL, *right =NULL, *first =NULL, *second=NULL;
while(head)
{
ListNode *new1 = new ListNode(head->val);
if(head->val<x)
{
if(!left) left =new1;
else
{ left->next=new1;
left = left->next;}
if(!first) first = new1;}
else
{if(!right) right =new1;
else
{right->next=new1;
right =right->next;}
if(!second) second = new1;}
head = head->next;
}
if(!left)
return second;
if(second)
left->next = second;
return first;
}
ListNode* partition(ListNode* head, int x) {
return helper(head, x);
}
};