-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathNextHigerValueNode.py
137 lines (104 loc) · 3.16 KB
/
NextHigerValueNode.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
# Python program to Point to next higher value node in a linked list with an arbitrary pointer
# Node class
class Node:
# Constructor to initialise data and next and arbitrary pointer
def __init__(self, data=None):
self.data = data
self.next = None
self.arbit = None
class SinglyLinkedList:
# Constructor to initialise head
def __init__(self, head=None):
self.head = head
# Function to Insert data at the beginning of the linked list
def insert_at_beg(self, data):
node = Node(data)
node.next = self.head
node.arbit = node.next
self.head = node
# Function to print the linked list
def print_data(self):
current = self.head
# print data of each node
while current is not None:
print(current.data, '-> ', end='')
current = current.next
current = self.head
print('None')
while current is not None:
print("V", ' ', end='')
current = current.next
current = self.head
print()
# print arbitrary pointer node's data of each node
while current is not None:
if current.arbit is not None:
print(current.arbit.data, ' ', end='')
else:
print("N", ' ', end='')
current = current.next
print()
# Function to split the linked list into two halves
def split(head):
slow = head
if slow is None or slow.arbit is None:
return head, None
fast = slow.arbit
# Reach to the middle of the linked list
while fast is not None:
fast = fast.arbit
if fast is not None:
fast = fast.arbit
slow = slow.arbit
fast = slow.arbit
# break the linked list in half
slow.arbit = None
# return the 2 linked lists formed
return head, fast
# Function to merge linked lists in sorted order
def merge(a, b):
# Make a dummy node
dummy = Node()
# dummy node arbit will be the head of our merged list
dummy.arbit = None
temp = SinglyLinkedList(dummy)
tail = temp.head
while True:
if a is None:
tail.arbit = b
break
elif b is None:
tail.arbit = a
break
elif a.data <= b.data:
tail.arbit = a
a = a.arbit
else:
tail.arbit = b
b = b.arbit
tail = tail.arbit
return temp.head.arbit
# Function Merge Sort
def merge_sort(head):
if head is None or head.arbit is None:
return head
a, b = split(head)
a = merge_sort(a)
b = merge_sort(b)
head = merge(a, b)
return head
if __name__ == '__main__':
linked_list = SinglyLinkedList()
linked_list.insert_at_beg(3)
linked_list.insert_at_beg(2)
linked_list.insert_at_beg(10)
linked_list.insert_at_beg(5)
# before linking the arbit
print('before linking')
linked_list.print_data()
# call merge_sort function
# to sort the linked list on the basis of the arbitrary pointers
merge_sort(linked_list.head)
# after linking the arbit
print('after linking')
linked_list.print_data()