/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @param x int整型
* @return ListNode类
*/
ListNode* partition(ListNode* head, int x) {
// write code here
if(head==NULL||head->next==NULL)
return head;
ListNode * head1=NULL,*head2=NULL;
ListNode*p1=NULL,*p2=NULL;
while(head){
if(head->val<x){
if(head1==NULL){
head1=head;
p1=head;
}
else{
p1->next=head;
p1=p1->next;
}
}
else{
if(head2==NULL){
head2=head;
p2=head;
}
else{
p2->next=head;
p2=p2->next;
}
}
head=head->next;
}
if(p2)
p2->next=NULL;
if(p1){//如果第一段链表不为空
p1->next=head2;
}
return head1==NULL?head2:head1;
}
};
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @param x int整型
* @return ListNode类
*/
ListNode* partition(ListNode* head, int x) {
// write code here
if(head==NULL||head->next==NULL)
return head;
ListNode * head1=NULL,*head2=NULL;
ListNode*p1=NULL,*p2=NULL;
while(head){
if(head->val<x){
if(head1==NULL){
head1=head;
p1=head;
}
else{
p1->next=head;
p1=p1->next;
}
}
else{
if(head2==NULL){
head2=head;
p2=head;
}
else{
p2->next=head;
p2=p2->next;
}
}
head=head->next;
}
if(p2)
p2->next=NULL;
if(p1){//如果第一段链表不为空
p1->next=head2;
}
return head1==NULL?head2:head1;
}
};