[Data structure and algorithm] leetcode148. Sorting linked list

Link:
Title:
Give you the head node head of the linked list, please sort it in ascending order and return the sorted linked list.
insert image description here


Solution:
space complexity: O(1), time complexity: O(n(logn)) —>>> recommended to use merge sort.
Space complexity: O(n), time complexity: O(n(logn)) —>>>It is recommended to use heap sort.
Heap sort, easy to understand.

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
struct cmp{
    
    
    bool operator()(int a, int b){
    
    
        return a>b;
    }
};
class Solution {
    
    
public:
    /**
     * 
     * @param head ListNode类 the head node
     * @return ListNode类
     */
    ListNode* sortInList(ListNode* head) {
    
    
        // write code here
        priority_queue<int,vector<int>,cmp> heap;
        while(head){
    
    
            heap.push(head->val);
            head=head->next;
        }
        ListNode* dhead=new ListNode(-1);
        ListNode* cur=dhead;
        while(!heap.empty()){
    
    
            cur->next=new ListNode(heap.top());heap.pop();
            cur=cur->next;
        }
        return dhead->next;
    }
};

Bottom-up merge sort

class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if (head == nullptr) {
            return head;
        }
        int length = 0;
        ListNode* node = head;
        while (node != nullptr) {
            length++;
            node = node->next;
        }
        ListNode* dummyHead = new ListNode(0, head);
        for (int subLength = 1; subLength < length; subLength <<= 1) {
            ListNode* prev = dummyHead, *curr = dummyHead->next;
            while (curr != nullptr) {
                ListNode* head1 = curr;
                for (int i = 1; i < subLength && curr->next != nullptr; i++) {
                    curr = curr->next;
                }
                ListNode* head2 = curr->next;
                curr->next = nullptr;
                curr = head2;
                for (int i = 1; i < subLength && curr != nullptr && curr->next != nullptr; i++) {
                    curr = curr->next;
                }
                ListNode* next = nullptr;
                if (curr != nullptr) {
                    next = curr->next;
                    curr->next = nullptr;
                }
                ListNode* merged = merge(head1, head2);
                prev->next = merged;
                while (prev->next != nullptr) {
                    prev = prev->next;
                }
                curr = next;
            }
        }
        return dummyHead->next;
    }

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


Guess you like

Origin blog.csdn.net/ryanji/article/details/127623937
Recommended