-
Notifications
You must be signed in to change notification settings - Fork 0
/
Partition List.cpp
42 lines (42 loc) · 981 Bytes
/
Partition List.cpp
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
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
if(head==NULL) return NULL;
ListNode *p=head,*p1=NULL,*t1=NULL,*p2=NULL,*t2=NULL;
while(p!=NULL){
if( p->val < x){
if(p1==NULL){
p1=p;
t1=p;
}
else{
t1->next=p;
t1=p;
}
}
else{
if(p2==NULL){
p2=p;
t2=p;
}
else{
t2->next=p;
t2=p;
}
}
p=p->next;
}
if(t1!=NULL) t1->next=NULL;
if(t2!=NULL) t2->next=NULL;
if( p1!=NULL && p2!=NULL ){
t1->next=p2;
return p1;
}
else if(p1!=NULL){
return p1;
}
else{
return p2;
}
}
};