-
Notifications
You must be signed in to change notification settings - Fork 0
/
Difference of two Linked Lists using Merge sort.py
166 lines (144 loc) · 4.24 KB
/
Difference of two Linked Lists using Merge sort.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""Given two Linked List, the task is to create a Linked List to store the difference of Linked List 1 with Linked List 2, i.e. the elements present in List 1 but not in List 2.
Examples:
Input:
List1: 10 -> 15 -> 4 ->20,
List2: 8 -> 4 -> 2 -> 10
Output: 15 -> 20
Explanation:
In the given linked list elements 15 and 20 are present in the list 1 but not in list 2.
Input:
List1: 2 -> 4 -> 8 -> 10,
List2: 8 -> 10
Output: 2 -> 4
Explanation:
In the given linked list 1 elements 2 and 4 are present in the list 1 but not in list 2.
The code is from geeksofgeeks and does not belong to me.
"""
# Node of the Linked List
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Linked List
class linked_list:
def __init__(self):
self.head = None
# Function to insert a node
# at the end of Linked List
def append(self, data):
temp = Node(data)
if self.head == None:
self.head = temp
else:
p = self.head
while p.next != None:
p = p.next
p.next = temp
# Function to find the middle
# node of the Linked List
def get_mid(self, head):
if head == None:
return head
slow = fast = head
while fast.next != None \
and fast.next.next != None:
slow = slow.next
fast = fast.next.next
return slow
# Recursive method to merge the
# two half after sorting
def merge(self, l, r):
if l == None:return r
if r == None:return l
if l.data<= r.data:
result = l
result.next = \
self.merge(l.next, r)
else:
result = r
result.next = \
self.merge(l, r.next)
return result
# Recursive method to divide the
# list into two half until 1 node left
def merge_sort(self, head):
if head == None or head.next == None:
return head
mid = self.get_mid(head)
next_to_mid = mid.next
mid.next = None
left = self.merge_sort(head)
right = self.merge_sort(next_to_mid)
sorted_merge = self.merge(left, right)
return sorted_merge
# Function to print the list elements
def display(self):
p = self.head
while p != None:
print(p.data, end =' ')
p = p.next
print()
# Function to get the difference list
def get_difference(p1, p2):
difference_list = linked_list()
# Scan the lists
while p1 != None and p2 != None:
# Condition to check if the
# Data of the both pointer are
# same then move ahead
if p2.data == p1.data:
p1 = p1.next
p2 = p2.next
# Condition to check if the
# Data of the first pointer is
# greater than second then
# move second pointer ahead
elif p2.data<p1.data:
p2 = p2.next
# Condition when first pointer
# data is greater than the
# second pointer then append
# into the difference list and move
else:
difference_list.append(p1.data)
p1 = p1.next
# If end of list2 is reached,
# there may be some nodes in
# List 1 left to be scanned,
# they all will be inserted
# in the difference list
if p2 == None:
while p1:
difference_list.append(p1.data)
p1 = p1.next
return difference_list
# Driver Code
if __name__ == '__main__':
# Linked List 1
list1 = linked_list()
list1.append(2)
list1.append(6)
list1.append(8)
list1.append(1)
# Linked List 2
list2 = linked_list()
list2.append(4)
list2.append(1)
list2.append(9)
# Sort both the linkedlists
list1.head = list1.merge_sort(
list1.head
)
list2.head = list2.merge_sort(
list2.head
)
# Get difference list
result = get_difference(
list1.head, list2.head
)
if result.head:
result.display()
# if difference list is empty,
# then lists are equal
else:
print('Lists are equal')