【second】Partition List
思路理清楚就好。
ListNode *partition(ListNode *head, int x) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(head==NULL)
return NULL;
ListNode* smallHead,*smallTail,*bigHead,*bigTail,*cur,*next;
smallHead = smallTail = bigHead = bigTail = NULL;
cur = head;
while(cur)
{
next = cur->next;
if(cur->val<x)
{
if(bigTail)
{
bigTail->next = next;
if(smallTail)
{
smallTail->next = cur;
cur->next = bigHead;
smallTail = cur;
}else
{
smallTail = smallHead = cur;
cur->next = bigHead;
}
}else
{
if(smallTail)
{
smallTail = cur;
}else
smallTail = smallHead = cur;
}
}else
{
if(bigTail)
bigTail = cur;
else
bigTail = bigHead = cur;
}
cur = next;
}
return (smallHead?smallHead:bigHead);
}
浙公网安备 33010602011771号