/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode *res = new ListNode(0);
res->next=head;
ListNode *temp=head, *pre=res;
ListNode *big=NULL, *prebig=NULL, *small=NULL, *presmall=NULL;
while (temp) {
if (temp->val>=x && big==NULL) {
prebig=pre;
big=temp;
}
if (temp->val<x && big!=NULL) {
presmall=pre;
small=temp;
temp=big;
if (big->next!=small) {
presmall->next=small->next;
small->next=big;
prebig->next=small;
prebig=small;
}else {
pre=small;
big->next=small->next;
small->next=big;
prebig->next=small;
prebig=small;
}
}
pre=temp;
temp=temp->next;
}
return res->next;
}
};
Partition List
最新推荐文章于 2021-04-06 09:37:45 发布