/**
* 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) {
vector<ListNode*> tmp_a;
vector<ListNode*> tmp_b;
if (head==NULL)
{
return NULL;
}
while (head!=NULL)
{
if (head->val<x)
tmp_a.push_back(head);
else
tmp_b.push_back(head);
head=head->next;
}
if (!tmp_a.empty())
{
for (int i=0;i<tmp_a.size()-1;i++)
{
tmp_a[i]->next = tmp_a[i+1];
}
}
if (!tmp_b.empty())
{
for (int j=0;j<tmp_b.size()-1;j++)
{
tmp_b[j]->next = tmp_b[j+1];
}
}
if ((!tmp_a.empty())&&(!tmp_b.empty()))
{
tmp_a[tmp_a.size()-1]->next=tmp_b[0];
tmp_b[tmp_b.size()-1]->next=NULL;
return tmp_a[0];
}
else if((!tmp_a.empty())&&(tmp_b.empty()))
{
tmp_a[tmp_a.size()-1]->next=NULL;
return tmp_a[0];
}
else
{
tmp_b[tmp_b.size()-1]->next=NULL;
return tmp_b[0];
}
}
};
Partition List
最新推荐文章于 2019-12-04 11:33:20 发布