返回 1->2->2->4->3->5->null
借鉴
class Solution {
public:
/**
* @param head: The first node of linked list.
* @param x: an integer
* @return: a ListNode
*/
ListNode *partition(ListNode *head, int x) {
ListNode *xx,*dd;
xx=head;
dd=head;
ListNode *first,*dummy;
first=new ListNode;
dummy=first;
while(xx!=NULL)
{
if(xx->val<x)
{
ListNode *s;
s=new ListNode;
s->val=xx->val;
first->next=s;
first=s;
}
xx=xx->next;
}
while(dd!=NULL)
{
if(dd->val>=x)
{
ListNode *s;
s=new ListNode;
s->val=dd->val;
first->next=s;
first=s;
}
dd=dd->next;
}
if(dummy==NULL)
{
return dummy;
}
else
return dummy->next;
// write your code here
}
};