-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path148_Sort_List.py
76 lines (65 loc) · 2.14 KB
/
148_Sort_List.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from typing import Optional
import pytest
from problems.utils import ListNode, list2linkedlist, linkedlist2list
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next: return head
slow, fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
fast = slow.next
slow.next = None
slow, fast = self.sortList(head), self.sortList(fast)
return self.merge(slow, fast)
def merge(self, left, right):
sentinel = cur = ListNode()
while left and right:
if left.val < right.val:
cur.next = left
left = left.next
else:
cur.next = right
right = right.next
cur = cur.next
if left:
cur.next = left
else:
cur.next = right
return sentinel.next
# faster way (O(n)) but with O(n) memory usage:
# def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
# vals = []
# cur = head
# while cur:
# vals.append(cur.val)
# cur = cur.next
# vals = sorted(vals)
# cur = head
# for val in vals:
# cur.val = val
# cur = cur.next
# return head
@pytest.mark.parametrize("linked_list,expected", [
([4, 2, 1, 3], [1, 2, 3, 4]),
([-1, 5, 3, 4, 0], [-1, 0, 3, 4, 5]),
([], []),
([-10 ** 5], [-10 ** 5]),
([10 ** 5], [10 ** 5]),
([*range(5 * 10 ** 4)], [*range(5 * 10 ** 4)]),
([*range(5 * 10 ** 4 - 1, -1, -1)], [*range(5 * 10 ** 4)]),
([*range(5 * 10 ** 4 - 1)] + [-10 ** 5],
[-10 ** 5] + [*range(5 * 10 ** 4 - 1)]),
([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
([8, 3, 6, 4, 2, 7, 0, 8, 9, 2], [0, 2, 2, 3, 4, 6, 7, 8, 8, 9])
])
def test_sort_list(linked_list, expected):
solution = Solution()
linked_list = list2linkedlist(linked_list)
res = solution.sortList(linked_list)
assert linkedlist2list(res) == expected