Título de cepillo 148. Ordenar lista

I, titulado Descripción

Título 148. Ordenar lista, la lista de la clase, el requisito de tiempo complejidad es O (n log (n)), una constante de los requisitos de complejidad espacial. La dificultad es media!

En segundo lugar, mi respuesta

A petición, la única norma es que la fusión de clasificación.

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

Las propiedades son las siguientes:

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.

En tercer lugar, las medidas de optimización

La función de fusión, modificada versión no recursiva:

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

Las propiedades son las siguientes:

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.

Supongo que te gusta

Origin www.cnblogs.com/siweihz/p/12274559.html
Recomendado
Clasificación