LeetCode 148. 排序链表

发布时间 2023-04-25 00:42:03作者: 垂序葎草

前置题目---21. 合并两个有序链表

原题解

题目

约束

题解

方法一


class Solution {
public:
    ListNode* sortList(ListNode* head) {
        return sortList(head, nullptr);
    }

    ListNode* sortList(ListNode* head, ListNode* tail) {
        if (head == nullptr) {
            return head;
        }
        if (head->next == tail) {
            head->next = nullptr;
            return head;
        }
        ListNode* slow = head, *fast = head;
        while (fast != tail) {
            slow = slow->next;
            fast = fast->next;
            if (fast != tail) {
                fast = fast->next;
            }
        }
        ListNode* mid = slow;
        return merge(sortList(head, mid), sortList(mid, tail));
    }

    ListNode* merge(ListNode* head1, ListNode* head2) {
        ListNode* dummyHead = new ListNode(0);
        ListNode* temp = dummyHead, *temp1 = head1, *temp2 = head2;
        while (temp1 != nullptr && temp2 != nullptr) {
            if (temp1->val <= temp2->val) {
                temp->next = temp1;
                temp1 = temp1->next;
            } else {
                temp->next = temp2;
                temp2 = temp2->next;
            }
            temp = temp->next;
        }
        if (temp1 != nullptr) {
            temp->next = temp1;
        } else if (temp2 != nullptr) {
            temp->next = temp2;
        }
        return dummyHead->next;
    }
};

方法二(常考)


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if(head == nullptr){
            return head;
        }
        int len = 0;
        ListNode* node = head;
        while(node != nullptr){
            len ++;
            node = node->next;
        }
        ListNode* dummy = new ListNode(0, head);
        for(int sublen = 1; sublen < len; sublen <<= 1){
            ListNode* pre = dummy,* curr = dummy->next;
            while(curr != nullptr){
                ListNode* head1 = curr;
                for(int i = 1; i < sublen && curr->next != nullptr; i++){
                    curr = curr->next;
                }

                ListNode* head2 = curr->next;
                curr->next = nullptr;
                curr = head2;
                for(int i = 1; i < sublen && curr != nullptr && curr->next != nullptr; i++){
                    curr = curr->next;
                }

                ListNode* next = nullptr;
                if(curr != nullptr){
                    next = curr->next;
                    curr->next = nullptr;
                }

                ListNode* merged = mergerList(head1, head2);
                pre->next = merged;
                while(pre->next != nullptr){
                    pre = pre->next;
                }

                curr = next;
            }

        }
        return dummy->next;
    }
    ListNode* mergerList(ListNode* head1, ListNode* head2){
        if(head1 == nullptr){
            return head2;
        }else if(head2 == nullptr){
            return head1;
        }else if(head1->val < head2->val){
            head1->next = mergerList(head1->next, head2);
            return head1;
        }else{
            head2->next = mergerList(head1, head2->next);
            return head2;
        }
    }
};