148. Sort List

本文介绍了一种使用常数空间复杂度,在O(n log n)时间内对链表进行排序的方法。通过迭代和递归两种方式实现链表排序,包括将链表拆分为两个部分并分别排序,最后将两个有序部分合并为一个有序链表的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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) {
        if ( head == NULL || head->next == NULL )
            return head;
            
        int length = 0;
        ListNode * cur = head;
        
        while(cur)
        {
            length ++ ;
            cur = cur->next;
        }
        
        ListNode dump(0);
        dump.next = head;
        ListNode *left = NULL;
        ListNode *right = NULL;
        ListNode *tail = NULL;
        
        for( int step=1; step<length; step<<=1 )
        {
            cur = dump.next;
            tail = &dump;
            while(cur)
            {
                left = cur;
                right = split(left, step);
                cur = split(right, step);
                tail = Merge(left, right, tail);
            }
        }
        return dump.next;
    }
    
    ListNode * split(ListNode *head, int step)
    {
        for(int i=1; head && i<step; i++) head = head->next;
        if ( !head ) return NULL;
        ListNode *second = head->next;
        head->next = NULL;
        return second;
    }
    
    ListNode * Merge(ListNode * left, ListNode * right, ListNode * tail )
    {
        ListNode * cur = tail;
        while (left && right)
        {
            if ( left->val < right->val )
            {
                 cur->next = left;
                 cur = left;
                 left = left->next;
                 
            }else
            {
                 cur->next = right;
                 cur = right;
                 right = right->next;
            }
        }
        
        cur->next = ( left ? left : right );
        while (cur->next) { cur = cur->next ;}
        return cur;
    }
};

递归方法:

class Solution {
public:
    ListNode* merge( ListNode* head1 , ListNode * head2){
        ListNode* d = new ListNode (0);            // dummy node
        ListNode* e = d;
        while(head1||head2){
            if(head1 && (!head2 || head1->val <= head2 -> val) ){
                e=e->next= head1 ; 
                head1 = head1 -> next;
            }
            if(head2 && (!head1 || head2->val < head1 -> val) ){
                e=e->next= head2 ; 
                head2 = head2 -> next;
            }
        }
        e->next = NULL;
        return d->next;
    }
   ListNode* sortList(ListNode* head) {
        if(!head || !head->next) return head;
        ListNode* slow = head;
        ListNode* fast =head->next;
         while(fast && fast->next){         // to find middle node     
            fast= fast->next->next;
            slow = slow->next;
        }
        ListNode* headb = slow->next;     // headb is start of 2nd half of list
        slow->next = NULL;                 
        return merge(sortList(head) , sortList(headb));
        }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值