1. 程式人生 > >單鏈表排序(sort list)

單鏈表排序(sort list)

今天在leetcode上看到一個很有趣的題目,用O(nlogn)的時間對連結串列進行排序,思考一下,覺得最好的方法也就是歸併排序了,其他排序都會對next指標有很大影響,操作起來很複雜。歸併,那麼需要找到連結串列的中間節點的位置,很顯然用兩個指標fast,slow一個走兩步一個走一步就能搞定了。

指標的問題看起來簡單,寫起來難,merge函式中忘記處理head指標,錯了一次,指標好用但難寫。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>

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

class Solution {
public:

    ListNode *findMid(ListNode *head) { //找出連結串列中間節點
        ListNode *fast = head->next;
        ListNode *slow = head;
        while(fast != NULL && fast->next != NULL){
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }

    ListNode* merge(ListNode *left,ListNode *right){ //歸併,注意處理的過程中head首節點為空,因此最後需要返回head->next
        ListNode *head = (ListNode *)malloc(sizeof(ListNode));
        ListNode *res = head; //注意保留head指標,這裡忘記處理WA了
        while (left != NULL && right != NULL) {
            if (left->val < right->val) {
                head->next = left;
                left = left->next;
            } else {
                head->next = right;
                right = right->next;
            }
            head = head->next;
        }
        if (left != NULL) {
            head->next = left;
        } else {
            head->next = right;
        }

        return res->next;
    }

    void createList(ListNode *&head){ //建立連結串列
        int x;
        while(scanf("%d",&x) != EOF){
            if(x == 0) break;
            ListNode *node = (ListNode *)malloc(sizeof(ListNode));
            node->next = NULL;
            node->val = x;
            if(head == NULL) {
                head = node;
            } else {
                node->next = head;
                head = node;
            }
        }
    }

    void printList(ListNode *head){ //列印
        while(head != NULL){
            cout<<head->val<<" ";
            head = head->next;
        }
        cout << endl;
    }

    ListNode* reverseList(ListNode *head){ //連結串列反轉
        if(head == NULL || head->next == NULL){
            return head;
        }
        ListNode *tail = head, *temp = NULL,*cur;
        while(tail->next != NULL){
            cur = tail->next;
            tail->next = temp;
            temp = tail;
            tail = cur;
        }
        tail->next = temp;
        return tail;
    }

    ListNode *sortList(ListNode *head) {
        if(head == NULL){
            return NULL;
        }
        if(head->next == NULL){
            return head;
        }
        ListNode *left,*right,*mid;
        mid = findMid(head);
        right = sortList(mid->next); //下面三行程式碼的順序需要注意
        mid->next = NULL;
        left = sortList(head);
        return merge(left,right);
    }
};

int main()
{
    ListNode *head = (ListNode *)malloc(sizeof(ListNode));
    head = NULL;
    Solution s;
    s.createList(head);
    s.printList(head);
    head = s.sortList(head);
    s.printList(head);
    head = s.reverseList(head);
    s.printList(head);
    return 0;
}