forked from girishgupta211-zz/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge-sort-linklist.py
137 lines (95 loc) · 2.51 KB
/
merge-sort-linklist.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# A simple Python program to sort linked list using merge sort
class Node:
# Function to initialise the node object
def __init__(self, data):
self.data = data # Assign data
self.next = None # Initialize next as null
# Linked List class contains a Node object
class LinkedList:
# Function to initialize head
def __init__(self):
self.head = None
def merge_sort(llist):
if llist is None:
return llist
if llist.next is None:
return llist
mid = llist
fast = llist
while fast and fast.next:
if fast.next.next is None:
break
mid = mid.next
fast = fast.next.next
first = llist
second = mid.next
mid.next = None
first = merge_sort(first)
second = merge_sort(second)
return merge_lists(first, second)
# 1, 3, 2 , 4 --> 1, 3 --> 1 -> 3 -> merge_lists(1,3) first(1,3) -->
# 2, 4 --> 2 -> 4 -> merge_lists(2, 4 ) second(2,4) -> merge_lists (1, 3 ) (2,4) -> (1, 2, 3, 4)
def merge_lists(llist1, llist2):
if llist2 is None:
return llist1
if llist1 is None:
return llist2
temp = None
if llist1.data < llist2.data:
temp = llist1
llist1 = llist1.next
else:
temp = llist2
llist2 = llist2.next
merged_list = temp
while llist1 and llist2:
if llist1.data < llist2.data:
temp.next = llist1
llist1 = llist1.next
else:
temp.next = llist2
llist2 = llist2.next
temp = temp.next
if llist1:
temp.next = llist1
if llist2:
temp.next = llist2
return merged_list
# Code execution starts here
if __name__ == '__main__':
# Start with the empty list
llist1 = LinkedList()
node1 = Node(1)
llist1.head = node1
node2 = Node(3)
node1.next = node2
node3 = Node(5)
node2.next = node3
node4 = Node(7)
node3.next = node4
node5 = Node(2)
node4.next = node5
# Start with the empty list
llist2 = LinkedList()
node1 = Node(2)
llist2.head = node1
node2 = Node(5)
node1.next = node2
node3 = Node(3)
node2.next = node3
node4 = Node(1)
node3.next = node4
print(llist1)
print(llist2)
print("Sort 1")
result = merge_sort(llist2.head)
temp = result
while temp:
print(temp.data)
temp = temp.next
print("Sort 2")
result = merge_sort(llist1.head)
temp = result
while temp:
print(temp.data)
temp = temp.next