Star

LeetCode 148. Sort List以及排序

排序算法: 时间复杂度为 O(nlgn):
quick sort (空间复杂度O(1)), merge sort(空间复杂度O(n)), heap sort (空间复杂度O(1)) 时间复杂度为 O(n): bucket sort, radix sort, 基于比较的排序,时间复杂度一般为O(nlgn)

Question

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

Explanation

这道题主要要掌握几种sort的方法怎么写,还有相关的复杂度分析。

Code

  1. Merge Sort: 时间复杂度为O(nlogn),空间复杂度O(n)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    public class Solution {
    public ListNode sortList(ListNode head) {
    if (head == null || head.next == null) return head;
    // Merge Sort
    ListNode mid = findMiddle(head);
    ListNode right = sortList(mid.next);
    mid.next = null;
    ListNode left = sortList(head);
    return mergeTwoLists(left, right);
    }
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    ListNode a = l1;
    ListNode b = l2;
    if (l1 == null && l2 == null) return null;
    ListNode curr = new ListNode(0);
    ListNode dummy= curr;
    while(a != null && b != null) {
    if (a.val < b.val) {
    dummy.next = new ListNode(a.val);
    a = a.next;
    } else {
    dummy.next = new ListNode(b.val);
    b = b.next;
    }
    dummy = dummy.next;
    }
    if(a!= null) dummy.next = a;
    else dummy.next = b;
    return curr.next;
    }
    private ListNode findMiddle(ListNode head) {
    ListNode walker = head;
    ListNode runner = head.next;
    while(runner!= null && runner.next!=null) {
    runner = runner.next.next;
    walker = walker.next;
    }
    return walker;
    }
    }

  2. Quick Sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class Solution {
public ListNode sortList(ListNode head) {
//Quick Sort
if(head == null || head.next == null) return head;
ListNode mid = findMedian(head);//O(n)
ListNode leftDummy = new ListNode(0), leftTail = leftDummy;
ListNode rightDummy = new ListNode(0), rightTail = rightDummy;
ListNode middleDummy = new ListNode(0), middleTail = middleDummy;
while (head != null) {
if (head.val < mid.val) {
leftTail.next = head;
leftTail = head;
} else if (head.val > mid.val) {
rightTail.next = head;
rightTail = head;
} else {
middleTail.next = head;
middleTail = head;
}
head = head.next;
}
leftTail.next = null;
middleTail.next = null;
rightTail.next = null;
ListNode left = sortList(leftDummy.next);
ListNode right = sortList(rightDummy.next);
return concat(left, middleDummy.next, right);
}
private ListNode findMedian(ListNode head) {
ListNode slow = head, fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
private ListNode concat(ListNode left, ListNode middle, ListNode right) {
ListNode dummy = new ListNode(0), tail = dummy;
tail.next = left; tail = getTail(tail);
tail.next = middle; tail = getTail(tail);
tail.next = right; tail = getTail(tail);
return dummy.next;
}
private ListNode getTail(ListNode head) {
if (head == null) {
return null;
}
while (head.next != null) {
head = head.next;
}
return head;
}
}