[LeetCode] 148. Sort List 链表排序

 

Sort a linked list in O(n log n) time using constant space complexity.html

Example 1:git

Input: 4->2->1->3
Output: 1->2->3->4

Example 2:github

Input: -1->5->3->4->0
Output: -1->0->3->4->5

 

常见排序方法有不少,插入排序,选择排序,堆排序,快速排序,冒泡排序,归并排序,桶排序等等。。它们的时间复杂度不尽相同,而这里题目限定了时间必须为O(nlgn),符合要求只有快速排序,归并排序,堆排序,而根据单链表的特色,最适于用归并排序。为啥呢?这是因为链表自身的特色决定的,因为不能经过坐标来直接访问元素,因此快排什么的可能不太容易实现(可是被评论区的大神们打脸,仍是能够实现的),堆排序的话,若是让新建结点的话,仍是能够考虑的,若只能交换结点,最好仍是不要用。而归并排序(又称混合排序)因其能够利用递归来交换数字,自然适合链表这种结构。归并排序的核心是一个 merge() 函数,其主要是合并两个有序链表,这个在 LeetCode 中也有单独的题目 Merge Two Sorted Lists。因为两个链表是要有序的才能比较容易 merge,那么对于一个无序的链表,如何才能拆分红有序的两个链表呢?咱们从简单来想,何时两个链表必定都是有序的?就是当两个链表各只有一个结点的时候,必定是有序的。而归并排序的核心实际上是分治法 Divide and Conquer,就是将链表从中间断开,分红两部分,左右两边再分别调用排序的递归函数 sortList(),获得各自有序的链表后,再进行 merge(),这样总体就是有序的了。由于子链表的递归函数中仍是会再次拆成两半,当拆到链表只有一个结点时,没法继续拆分了,而这正好知足了前面所说的“一个结点的时候必定是有序的”,这样就能够进行 merge 了。而后再回溯回去,每次获得的都是有序的链表,而后进行 merge,直到还原整个长度。这里将链表从中间断开的方法,采用的就是快慢指针,你们可能对快慢指针找链表中的环比较熟悉,其实找链表中的中点一样好使,由于快指针每次走两步,慢指针每次走一步,当快指针到达链表末尾时,慢指针正好走到中间位置,参见代码以下:ide

 

C++ 解法一:函数

class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if (!head || !head->next) return head;
        ListNode *slow = head, *fast = head, *pre = head;
        while (fast && fast->next) {
            pre = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        pre->next = NULL;
        return merge(sortList(head), sortList(slow));
    }
    ListNode* merge(ListNode* l1, ListNode* l2) {
        ListNode *dummy = new ListNode(-1);
        ListNode *cur = dummy;
        while (l1 && l2) {
            if (l1->val < l2->val) {
                cur->next = l1;
                l1 = l1->next;
            } else {
                cur->next = l2;
                l2 = l2->next;
            }
            cur = cur->next;
        }
        if (l1) cur->next = l1;
        if (l2) cur->next = l2;
        return dummy->next;
    }
};

 

Java 解法一:post

public class Solution {
    public ListNode sortList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode slow = head, fast = head, pre = head;
        while (fast != null && fast.next != null) {
            pre = slow;
            slow = slow.next;
            fast = fast.next.next;
        }
        pre.next = null;
        return merge(sortList(head), sortList(slow));
    }
    public ListNode merge(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                cur.next = l1;
                l1 = l1.next;
            } else {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        if (l1 != null) cur.next = l1;
        if (l2 != null) cur.next = l2;
        return dummy.next;
    }
}

 

下面这种方法也是归并排序,并且在merge函数中也使用了递归,这样使代码更加简洁啦~ui

 

C++ 解法二:url

class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if (!head || !head->next) return head;
        ListNode *slow = head, *fast = head, *pre = head;
        while (fast && fast->next) {
            pre = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        pre->next = NULL;
        return merge(sortList(head), sortList(slow));
    }
    ListNode* merge(ListNode* l1, ListNode* l2) {
        if (!l1) return l2;
        if (!l2) return l1;
        if (l1->val < l2->val) {
            l1->next = merge(l1->next, l2);
            return l1;
        } else {
            l2->next = merge(l1, l2->next);
            return l2;
        }
    }
};

 

Java 解法二:spa

public class Solution {
    public ListNode sortList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode slow = head, fast = head, pre = head;
        while (fast != null && fast.next != null) {
            pre = slow;
            slow = slow.next;
            fast = fast.next.next;
        }
        pre.next = null;
        return merge(sortList(head), sortList(slow));
    }
    public ListNode merge(ListNode l1, ListNode l2) {
        if (l1 == null) return l2;
        if (l2 == null) return l1;
        if (l1.val < l2.val) {
            l1.next = merge(l1.next, l2);
            return l1;
        } else {
            l2.next = merge(l1, l2.next);
            return l2;
        }
    }
}

 

Github 同步地址:指针

https://github.com/grandyang/leetcode/issues/148

 

相似题目:

Merge Two Sorted Lists

Sort Colors

Insertion Sort List

 

参考资料:

https://leetcode.com/problems/sort-list/description/

https://leetcode.com/problems/sort-list/discuss/46857/clean-and-short-merge-sort-solution-in-c

https://leetcode.com/problems/sort-list/discuss/46937/56ms-c-solutions-using-quicksort-with-explanations

https://leetcode.com/problems/sort-list/discuss/46772/i-have-a-pretty-good-mergesort-method-can-anyone-speed-up-the-run-time-or-reduce-the-memory-usage

 

LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索