-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_lists.py
87 lines (67 loc) · 1.87 KB
/
linked_lists.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
77
78
79
80
81
82
83
84
85
86
87
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, value):
new_node = Node(value)
if not self.head:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
def print_list(self):
current = self.head
while current:
print(current.value, end=" ")
current = current.next
print()
def merge_sort(self):
self.head = self.__merge_sort(self.head)
def __merge_sort(self, head): # O(n log n)
if not head or not head.next:
return head
middle = self.__get_middle(head)
next_to_middle = middle.next
middle.next = None
left = self.__merge_sort(head)
right = self.__merge_sort(next_to_middle)
return self.__sorted_merge(left, right)
def __get_middle(self, head): # O(n)
if not head:
return head
slow = head
fast = head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
def __sorted_merge(self, left, right): # O(n)
if not left:
return right
if not right:
return left
if left.value <= right.value:
result = left
result.next = self.__sorted_merge(left.next, right)
else:
result = right
result.next = self.__sorted_merge(left, right.next)
return result
list = LinkedList()
list.append(38)
list.append(27)
list.append(43)
list.append(3)
list.append(9)
list.append(82)
list.append(10)
print("Before:")
list.print_list()
list.merge_sort()
print("After:")
list.print_list()