首页 > 其他 > 详细

【Leetcode】Sort List

时间:2014-08-20 14:03:02      阅读:221      评论:0      收藏:0      [点我收藏+]

Sort a linked list in O(n log n) time using constant space complexity.

 

1st  ( 7 tries)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution 
{
public:
    ListNode *sortList(ListNode *head) 
    {
        if(head == NULL)
            return NULL;
        if(head->next == NULL)
            return head;
            
        ListNode* middle = head,*middlecur = middle;
        
        ListNode* littlehead = NULL,*littlecur = NULL;
        ListNode* bighead = NULL,*bigcur = NULL;
        ListNode* cur = middle->next;
        
        while(cur != NULL)
        {
            if(cur->val < middle->val)
            {
                if(littlehead == NULL)
				{
                    littlehead = cur;
					littlecur = cur;
				}
                else
				{
                    littlecur->next = cur;
					littlecur = littlecur->next;
				}
            }
            else if(cur->val > middle->val)
            {
                if(bighead == NULL)
				{
                    bighead = cur;
					bigcur = cur;
				}
                else
				{
                    bigcur->next = cur;
					bigcur = bigcur->next;
				}
            }
            else 
            {
                middlecur->next = cur;
                middlecur = middlecur->next;
            }
            cur = cur->next;
        }
        if(littlecur != NULL)
            littlecur->next = NULL;
        if(bigcur != NULL)
            bigcur->next = NULL;
        
        ListNode* rel = sortList(littlehead);
        cur = rel;
        ListNode* reb = sortList(bighead);
        middlecur->next = reb;
        if(rel == NULL)
            return middle;
        else
        {
            while(cur->next != NULL)
                cur = cur->next;
            cur->next = middle;
            return rel;
        }
    }
};

  

2nd ( 17 tries)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *sortList(ListNode *head) {
        //quick sort???
        return sortList_ht(head).first;
    }
    //head && tail
    pair<ListNode*,ListNode*> sortList_ht(ListNode *head) {
        if(head == NULL)
            return pair<ListNode*,ListNode*>(NULL,NULL);
        if(head->next == NULL)
            return pair<ListNode*,ListNode*>(head,head);
        ListNode *partition_b = head,*partition_e = head;
        ListNode *cur = head->next;
        ListNode *hs = NULL,*hb = NULL,*ts = NULL,*tb = NULL;
        while(cur != NULL) {
            if(cur->val < partition_b->val) {
                if(hs == NULL) {
                    hs = cur;
                    ts = cur;
                }
                else {
                    ts->next = cur;
                    ts = ts->next;
                }
            }
            else if(cur->val > partition_b->val) {
                if(hb == NULL) {
                    hb = cur;
                    tb = cur;
                }
                else {
                    tb->next = cur;
                    tb = tb->next;
                }
            }
            else {
                partition_e->next = cur;
                partition_e = partition_e->next;
            }
            cur = cur->next;
        }
        if(ts != NULL)
            ts->next = NULL;
        if(tb != NULL)
            tb->next = NULL;
        pair<ListNode*,ListNode*> ps = sortList_ht(hs);
        pair<ListNode*,ListNode*> pb = sortList_ht(hb);
        ListNode *rh,*rt;
        if(ps.second == NULL) {
            rh = partition_b;
        }
        else {
            rh = ps.first;
            ps.second->next = partition_b;
        }
        if(pb.first == NULL) {
            rt = partition_e;
            partition_e->next = NULL;
        }
        else {
            rt = pb.second;
            partition_e->next = pb.first;
        }
        return pair<ListNode*,ListNode*>(rh,rt);
    }
};

  

【Leetcode】Sort List,布布扣,bubuko.com

【Leetcode】Sort List

原文:http://www.cnblogs.com/weixliu/p/3924372.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!