|
| 1 | +''' |
| 2 | +Descripttion: 排序链表 |
| 3 | +version: 1 |
| 4 | +Author: Jason |
| 5 | +Date: 2020-11-21 08:10:16 |
| 6 | +LastEditors: Jason |
| 7 | +LastEditTime: 2020-11-21 11:48:00 |
| 8 | +''' |
| 9 | +import random |
| 10 | + |
| 11 | + |
| 12 | +def GenerateRandomList(number, size): |
| 13 | + temp = list() |
| 14 | + random_legth = random.randint(0, size) |
| 15 | + current_length = 0 |
| 16 | + while current_length < random_legth: |
| 17 | + temp.append(random.randint(1, number)) |
| 18 | + current_length += 1 |
| 19 | + return temp |
| 20 | + |
| 21 | + |
| 22 | +class ListNode: |
| 23 | + def __init__(self, x): |
| 24 | + self.val = x |
| 25 | + self.next = None |
| 26 | + |
| 27 | + |
| 28 | +class Solution: |
| 29 | + def sortList(self, head: ListNode) -> ListNode: |
| 30 | + if not head or not head.next: |
| 31 | + return head |
| 32 | + |
| 33 | + mid = self.getMid(head) |
| 34 | + right_head = mid.next |
| 35 | + mid.next = None |
| 36 | + |
| 37 | + left = self.sortList(head) |
| 38 | + right = self.sortList(right_head) |
| 39 | + |
| 40 | + return self.merge(left, right) |
| 41 | + |
| 42 | + def getMid(self, head: ListNode) -> ListNode: |
| 43 | + # 找到中点-slow |
| 44 | + slow = head |
| 45 | + fast = head |
| 46 | + # 通过以下while循环,找到链表的中点-slow |
| 47 | + while fast.next: |
| 48 | + if fast.next: |
| 49 | + fast = fast.next |
| 50 | + else: |
| 51 | + break |
| 52 | + if fast.next: |
| 53 | + fast = fast.next |
| 54 | + else: |
| 55 | + break |
| 56 | + slow = slow.next |
| 57 | + return slow |
| 58 | + |
| 59 | + def merge(self, left_head: ListNode, right_head: ListNode) -> ListNode: |
| 60 | + left_cur = left_head |
| 61 | + right_cur = right_head |
| 62 | + dummy_head = ListNode(float('-inf')) |
| 63 | + cur = dummy_head |
| 64 | + while left_cur and right_cur: |
| 65 | + if left_cur.val < right_cur.val: |
| 66 | + cur.next = left_cur |
| 67 | + left_cur = left_cur.next |
| 68 | + else: |
| 69 | + cur.next = right_cur |
| 70 | + right_cur = right_cur.next |
| 71 | + cur = cur.next |
| 72 | + cur.next = left_cur if left_cur else right_cur |
| 73 | + return dummy_head.next |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + s = Solution() |
| 78 | + for _ in range(100): |
| 79 | + nums = GenerateRandomList(20, 10) |
| 80 | + if len(nums) < 1: |
| 81 | + continue |
| 82 | + nodes = [ListNode(each_num) for each_num in nums] |
| 83 | + sorted_nums = list(sorted(nums)) |
| 84 | + # 连接链表 |
| 85 | + for i in range(len(nodes) - 1): |
| 86 | + nodes[i].next = nodes[i + 1] |
| 87 | + res = s.sortList(nodes[0]) |
| 88 | + insertsort_li = list() |
| 89 | + cur = res |
| 90 | + while cur: |
| 91 | + insertsort_li.append(cur.val) |
| 92 | + cur = cur.next |
| 93 | + if insertsort_li != sorted_nums: |
| 94 | + print("wrong:", nums) |
| 95 | + print("Done") |
| 96 | + # li1 = [3, 1, 12, 6, 1, 9, 19, 4, 12] |
| 97 | + # li2 = [2, 4, 6, 8, 10] |
| 98 | + # nodes1 = [ListNode(each_num) for each_num in li1] |
| 99 | + # nodes2 = [ListNode(each_num) for each_num in li2] |
| 100 | + # # 连接链表 |
| 101 | + # for i in range(len(nodes1) - 1): |
| 102 | + # nodes1[i].next = nodes1[i + 1] |
| 103 | + # for j in range(len(nodes2) - 1): |
| 104 | + # nodes2[j].next = nodes2[j + 1] |
| 105 | + # insertsort_li = list() |
| 106 | + # res = s.sortList(nodes1[0]) |
| 107 | + # cur = res |
| 108 | + # while cur: |
| 109 | + # insertsort_li.append(cur.val) |
| 110 | + # cur = cur.next |
| 111 | + # if insertsort_li != sorted(li1): |
| 112 | + # print("wrong:") |
0 commit comments