class Solution {
public:
ListNode* partition(ListNode* head, int x) {
if(!head||!head->next)
return head;
ListNode *less=NULL;
ListNode *more=NULL;
ListNode *morehead=NULL;
ListNode *lesshead=NULL;
while(head)
{
if(head->val<x)
{
if(!less) {
less = head;
lesshead=head;
head=head->next;
}
else
{
less->next=head;
less=less->next;
head=head->next;
}
}
else
{
if(!more)
{
more=head;
morehead=head;
head=head->next;
}
else
{
more->next=head;
more=more->next;
head=head->next;
}
}
}
if(!morehead)
return lesshead;
else if(!lesshead)
return morehead;
more->next=NULL;
less->next=morehead;
return lesshead;
}
};
86. Partition List
最新推荐文章于 2023-06-24 15:47:04 发布