【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);
        
    }

  

posted @ 2013-10-25 14:54  summer_zhou  阅读(130)  评论(0编辑  收藏  举报