LeetCode | Sort List

题目:

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



思路:

合并排序法。


代码:

/**
 * 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) {
        bool count = false;
        int sum =0;
        int run = 0;
        int step = 1;
        ListNode * begin = new ListNode(INT_MIN);
        begin->next = head;
        do
        {
            ListNode * p = begin;
            while(p->next!=NULL)
            {
                if(!count)
                    run++;
                ListNode * slow = p->next;
                int slowstep = step;
                ListNode * fast = goNext(slow, step);
                int faststep = step;
                while(slow!=NULL && fast!=NULL && slowstep>0 && faststep >0)
                {
                    p->next = slow->val<fast->val?slow:fast;
                    p=p->next;
                    if(slow->val<fast->val)
                    {
                        slow=slow->next;
                        slowstep--;
                    }
                    else
                    {
                        fast=fast->next;
                        faststep--;
                    }
                }
                while(slow!=NULL&&slowstep>0)
                {
                    p->next = slow;
                    p=p->next;
                    slow=slow->next;
                    slowstep--;
                }
                while(fast!=NULL&&faststep>0)
                {
                    p->next = fast;
                    p=p->next;
                    fast=fast->next;
                    faststep--;
                }
                p->next=fast;
            }
            count = true;
            sum = run*2;
            step*=2;
        }while(step<sum);
        return  begin->next;
    }
    
    ListNode* goNext(ListNode * p, int n)
    {
        while(n-->0)
        {
            if(p==NULL)
                break;
            else
                p=p->next;
        }
        return p;
    }
};

另一种实现方式:

/**
 * 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;
        }
        
        int step = 1;
        while(mergeSort(head, step)){
            step *= 2;
        };
        
        return head;
    }
    
    bool mergeSort(ListNode* &head, int step){
        ListNode *p1 = head;
        ListNode *p2 = head;
        
        for(int i = 0; i < step; i++){
            if(p2->next == NULL){
                return false;
            }
            else{
                p2 = p2->next;
            }
        }
        
        int step1 = step;
        int step2 = step;
        head = NULL;
        ListNode *p = NULL;
        
        while(step1 > 0 && (step2 > 0 && p2 != NULL)){
            if(p1->val < p2->val){
                setNext(head, p, p1);
                p1 = p1->next;
                step1--;
            }
            else{
                setNext(head, p, p2);
                p2 = p2->next;
                step2--;
            }
        };
        
        while(step1 > 0){
            setNext(head, p, p1);
            p1 = p1->next;
            step1--;
        };
        
        while(step2 > 0 && p2 != NULL){
            setNext(head, p, p2);
            p2 = p2->next;
            step2--;
        };
        
        if(p2 != NULL){
            mergeSort(p2, step);
        }
        p->next = p2;

        return true;
    }
    
    void setNext(ListNode* &head, ListNode* &cursor, ListNode* &value){
        if(cursor == NULL){
            cursor = value;
            head = cursor;
        }
        else{
            cursor->next = value;
            cursor = cursor->next;
        }
    }
};

    原文作者:Allanxl
    原文地址: https://blog.csdn.net/lanxu_yy/article/details/17308791
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞