链表的归并排序与快速排序

链表数据结构

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

链表归并排序

class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if (head == nullptr || head->next == nullptr)
            return head;
        ListNode *fast = head, *slow = head;
        while (fast->next && fast->next->next)
        {
            slow = slow->next;
            fast = fast->next->next;
        }

        fast = slow->next;
        slow->next = nullptr;
        head = sortList(head);
        fast = sortList(fast);
        return merge(head, fast);
    }

private:
    ListNode* merge(ListNode *l1, ListNode *l2)
    {
        ListNode empty(-1);
        ListNode *tail = ∅
        ListNode *tmp;
        for (; l1 || l2; tail = tail->next)
        {
            if (l1 == nullptr)
            {
                tail->next = l2;
                break;
            }
            if (l2 == nullptr)
            {
                tail->next = l1;
                break;
            }
            if (l1->val <= l2->val)
            {
                tmp = l1;
                l1 = l1->next;
            }
            else
            {
                tmp = l2;
                l2 = l2->next;
            }
            tail->next = tmp;
        }

        return empty.next;
    }
};

基于值交换的链表快速排序

class Solution {
public:
    ListNode* sortList(ListNode* head) {
        return sort(head, nullptr);
    }
private:
    void swap(ListNode *a, ListNode *b)
    {
        int tmp = a->val;
        a->val = b->val;
        b->val = tmp;
    }
    ListNode *sort(ListNode *begin, ListNode *end)
    {
        if (begin == nullptr || begin == end || begin->next == end)
            return begin;
        int key = begin->val;
        ListNode *p = begin;
        ListNode *q = begin->next;
        for (; q != end; q = q->next)
        {
            if (q->val < key)
            {
                p = p->next;
                swap(p, q);
            }
        }
        swap(p, begin);
        sort(begin, p);
        sort(p->next, end);

        return begin;
    }
};


基于节点交换的链表快速排序

class Solution {
public:
    ListNode* sortList(ListNode* head) {
        return sort(head, nullptr);
    }
private:
    ListNode *sort(ListNode *begin, ListNode *end)
    {
        if (begin == nullptr || begin == end || begin->next == end)
            return begin;
        ListNode *part = patition(begin, end);

        ListNode *first = sort(begin, part);
        ListNode *second = sort(part->next, end);
        part->next = second;

        return first;
    }

    ListNode *patition(ListNode * &begin, ListNode *end)
    {
        ListNode *head = begin;
        ListNode *tail = begin;
        ListNode *cur = begin->next;
        while (cur != end)
        {
            if (cur->val < begin->val)
            {
                tail->next = cur->next;
                cur->next = head;
                head = cur;
                cur = tail;
            }

            tail = cur;
            cur = cur->next;
        }
        ListNode *part = begin;
        begin = head;
        return part;
    }
};

 

posted on 2016-08-10 15:03  lakeone  阅读(243)  评论(0编辑  收藏  举报

导航