-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_0148.py
73 lines (53 loc) · 1.82 KB
/
leetcode_0148.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
# Todo: sort linked list not list(merge sort & quick sort)
from typing import Optional
from random import randint
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def linkedListToList(self, curr: ListNode) -> list[int]:
result: list[int] = []
while curr:
result.append(curr.val)
curr = curr.next
return result
def updateLinkedList(self, curr: ListNode, sorted_list: list[int]) -> None:
for num in sorted_list:
if curr:
curr.val = num
curr = curr.next
def quick_sort(self, arr: list[int]):
length = len(arr)
# base case
if length < 2:
return arr
# select a pivot
pivot_index = randint(0, length - 1)
pivot = arr[pivot_index]
# define right and left lists
left: list[int] = []
right: list[int] = []
# find all values greater or equal and smaller than pivot
for num in arr[:pivot_index] + arr[pivot_index + 1:]:
if num < pivot:
left.append(num)
else:
right.append(num)
return (self.quick_sort(left)) + [pivot] + self.quick_sort(right)
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
numbers: list[int] = self.linkedListToList(head)
# numbers = self.quick_sort(numbers)
numbers.sort()
self.updateLinkedList(head, numbers)
return head
def create_linked_list(numbers: list[int]):
head = ListNode(numbers[0])
curr = head
for num in numbers[1:]:
curr.next = ListNode(num)
curr = curr.next
return head
s = Solution()
print(s.sortList(create_linked_list([4, 2, 1, 3])))