Brush title 148. Sort List

I, entitled Description

Title 148. Sort List, the list of the sort, the time complexity requirement is O (nlog (n)), a constant spatial complexity requirements. The difficulty is Medium!

Second, my answer

Upon request, the only standard is that merge sort.

class Solution{
    public:
        ListNode* sortList(ListNode* head){
            if(head==NULL || head->next==NULL) return head;
            return mergeSort(head);
        }
        //归并排序 
        ListNode* mergeSort(ListNode* node){
            if(node==NULL || node->next==NULL) return node;
            ListNode* fast= node,*slow = node;
            ListNode* breakNode = node;//breakNode 指向l1的最后一个元素 
            //也可以采用先遍历一遍,统计链表节点的数量,然后归并排序
            //找到链表中间 
            while(fast && fast->next){
                fast = fast->next->next;
                breakNode = slow;
                slow = slow->next;
            }
            breakNode->next = NULL;
            ListNode* l1 = mergeSort(node);
            ListNode* l2 = mergeSort(slow);
            
            return merge(l1,l2);
        } 
        
        //合并两个链表 recursive
        ListNode* merge(ListNode* l1,ListNode* l2){
            if(l1 == NULL){
                return l2;
            }
            if(l2 == NULL){
                return l1;
            }
            if(l1->val < l2->val){
                l1->next = merge(l1->next,l2);
                return l1;
            }else{
                l2->next = merge(l2->next,l1);
                return l2;
            }
        } 
};

Properties are as follows:

Runtime: 60 ms, faster than 41.32% of C++ online submissions for Sort List.
Memory Usage: 16.2 MB, less than 15.00% of C++ online submissions for Sort List.

Third, the optimization measures

The merge function, modified non-recursive version:

class Solution{
    public:
        ListNode* sortList(ListNode* head){
            if(head==NULL || head->next==NULL) return head;
            return mergeSort(head);
        }
        //归并排序 
        ListNode* mergeSort(ListNode* node){
            if(node==NULL || node->next==NULL) return node;
            ListNode* fast= node,*slow = node;
            ListNode* breakNode = node;//breakNode 指向l1的最后一个元素 
            //找到链表中间 
            while(fast && fast->next){
                fast = fast->next->next;
                breakNode = slow;
                slow = slow->next;
            }
            breakNode->next = NULL;
            ListNode* l1 = mergeSort(node);
            ListNode* l2 = mergeSort(slow);
            
            return merge(l1,l2);
        } 
        //合并两个链表
        ListNode* merge(ListNode* l1,ListNode* l2){
            if(l1 == NULL) return l2;
            if(l2 == NULL) return l1;
            ListNode dummy(0);
            ListNode* p = &dummy;
            while(l1!=NULL && l2!=NULL){
                if(l1->val < l2->val){
                    p->next = l1;
                    l1 = l1->next;  
                }else{
                    p->next = l2;
                    l2 = l2->next;
                }
                p = p->next;
            }
            if(l1 !=NULL){
                p->next = l1;
            }
            if(l2 !=NULL){
                p->next = l2;
            }
            return dummy.next;
        }
};

Properties are as follows:

Runtime: 52 ms, faster than 67.44% of C++ online submissions for Sort List.
Memory Usage: 15 MB, less than 15.00% of C++ online submissions for Sort List.

Guess you like

Origin www.cnblogs.com/siweihz/p/12274559.html