AcWing 1451. singly linked list Quick Sort

Title Description

Original title link
Here Insert Picture Description
Here Insert Picture Description
time complexity: THE ( n l The g n ) O (nlogn) can be obtained with the master theorem
spatial complexity: THE ( l The g n ) O (logn) take up much stack space

Description:

  • The following list quicksort single writing can be assured that sorting is stable, because it is not based on a sorting exchanged quickly sort different from this array
  • If the required time complexity THE ( n l The g n ) O (nlogn) , the spatial complexity THE ( 1 ) O (1) , must be used in the form of iteration merge sort

C ++ code

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getTail(ListNode *head) {
        while (head->next)
            head = head->next;
        return head;
    }

    ListNode *quickSortList(ListNode *head) {
        // 遍历链表,按照三种情况分别加到三个链表中

        // 空结点或单结点
        if (!head || !head->next) return head;
        ListNode *lhead = new ListNode(-1), *mhead = new ListNode(-1), *rhead = new ListNode(-1);
        auto left = lhead, mid = mhead, right = rhead;

        // 遍历链表
        int x = head->val;
        for (auto p = head; p; p = p->next) {
            if (p->val < x)
                left = left->next = p;
            else if (p->val == x)
                mid = mid->next = p;
            else
                right = right->next = p;
        }

        // 得到三个链表,递归左边和右边链表
        left->next = mid->next = right->next = nullptr;
        lhead->next = quickSortList(lhead->next);
        rhead->next = quickSortList(rhead->next);
        // 拼接链表
        getTail(lhead)->next = mhead->next;
        getTail(mhead)->next = rhead->next;
        
        auto re = lhead->next;
        delete lhead;
        delete mhead;
        delete rhead;
        
        return re;
    }
};

The complete code

#include <iostream>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x)
        : val(x)
        , next(NULL) {
    }
};

// create linked list, -1 means NULL
ListNode *createList() {
    int d;
    cin >> d;
    if (d == -1) return NULL;
    ListNode *head = new ListNode(d);
    head->next = createList();
    return head;
}

ListNode *getTail(ListNode *head) {
    while (head->next)
        head = head->next;
    return head;
}

ListNode *quickSortList(ListNode *head) {
    // 遍历链表,按照三种情况分别加到三个链表中

    // 空结点或单结点
    if (!head || !head->next) return head;
    ListNode *lhead = new ListNode(-1), *mhead = new ListNode(-1), *rhead = new ListNode(-1);
    auto left = lhead, mid = mhead, right = rhead;

    // 遍历链表
    int x = head->val;
    for (auto p = head; p; p = p->next) {
        if (p->val < x)
            left = left->next = p;
        else if (p->val == x)
            mid = mid->next = p;
        else
            right = right->next = p;
    }

    // 得到三个链表,递归左边和右边链表
    left->next = mid->next = right->next = nullptr;
    lhead->next = quickSortList(lhead->next);
    rhead->next = quickSortList(rhead->next);
    // 拼接链表
    getTail(lhead)->next = mhead->next;
    getTail(mhead)->next = rhead->next;

    auto re = lhead->next;
    delete lhead;
    delete mhead;
    delete rhead;

    return re;
}

int main() {
    auto head = createList();
    head = quickSortList(head);

    while (head) {
        cout << head->val << " ";
        head = head->next;
    }
    return 0;
}


Written in the last : my blog mainly on the field of computer science summarized knowledge of thinking, and review, to write each blog it is easy to understand my goal, sharing technology and knowledge is a pleasure , and I welcome everyone together with the exchange of learning, there can be no question in the comments area, but also look forward to in-depth exchanges with your (^ ∀ ^ ●)

Published 308 original articles · won praise 149 · Views 150,000 +

Guess you like

Origin blog.csdn.net/qq_43827595/article/details/105012858