Skip to content

Commit f20f028

Browse files
Merging linked lists in Python
1 parent 7165ad7 commit f20f028

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of DSA course available on CourseGalaxy.com
3+
4+
class Node:
5+
6+
def __init__(self,value):
7+
self.info = value
8+
self.link = None
9+
10+
11+
class SingleLinkedList:
12+
13+
def __init__(self):
14+
self.start = None
15+
16+
def display_list(self):
17+
if self.start is None:
18+
print("List is empty")
19+
return
20+
else:
21+
print("List is : ")
22+
p = self.start
23+
while p is not None:
24+
print(p.info , " ", end='')
25+
p = p.link
26+
print()
27+
28+
def insert_at_end(self, data):
29+
temp = Node(data)
30+
if self.start is None:
31+
self.start = temp
32+
return
33+
34+
p = self.start
35+
while p.link is not None:
36+
p = p.link
37+
p.link = temp
38+
39+
40+
def create_list(self):
41+
n = int(input("Enter the number of nodes : "))
42+
if n == 0:
43+
return
44+
for i in range(n):
45+
data = int(input("Enter the element to be inserted : "))
46+
self.insert_at_end(data)
47+
48+
49+
def bubble_sort_exdata(self):
50+
end = None
51+
while end != self.start.link:
52+
53+
p = self.start
54+
while p.link != end:
55+
q = p.link
56+
if p.info > q.info:
57+
p.info,q.info = q.info,p.info
58+
p = p.link
59+
end = p
60+
61+
def merge1(self, list2):
62+
merge_list = SingleLinkedList()
63+
merge_list.start = self._merge1(self.start, list2.start)
64+
return merge_list
65+
66+
def _merge1(self, p1, p2):
67+
if p1.info <= p2.info:
68+
startM = Node(p1.info)
69+
p1 = p1.link
70+
else:
71+
startM = Node(p2.info)
72+
p2 = p2.link;
73+
74+
pM = startM
75+
76+
while p1 is not None and p2 is not None:
77+
if p1.info <= p2.info :
78+
pM.link = Node(p1.info)
79+
p1 = p1.link
80+
else:
81+
pM.link = Node(p2.info);
82+
p2 = p2.link;
83+
pM = pM.link;
84+
85+
86+
#If second list has finished and elements left in first list
87+
while p1 is not None:
88+
pM.link = Node(p1.info)
89+
p1 = p1.link
90+
pM = pM.link
91+
92+
#If first list has finished and elements left in second list
93+
while p2 is not None:
94+
pM.link = Node(p2.info)
95+
p2 = p2.link
96+
pM = pM.link
97+
98+
return startM
99+
100+
101+
def merge2(self,list2):
102+
merge_list = SingleLinkedList()
103+
merge_list.start = self._merge2(self.start, list2.start)
104+
return merge_list
105+
106+
def _merge2(self, p1, p2):
107+
108+
if p1.info <= p2.info:
109+
startM = p1
110+
p1 = p1.link
111+
else:
112+
startM = p2
113+
p2 = p2.link
114+
115+
pM = startM
116+
117+
while p1 is not None and p2 is not None:
118+
if p1.info <= p2.info :
119+
pM.link = p1
120+
pM = pM.link
121+
p1 = p1.link
122+
else:
123+
pM.link = p2
124+
pM = pM.link
125+
p2 = p2.link
126+
127+
if p1 is None:
128+
pM.link = p2
129+
else:
130+
pM.link = p1
131+
132+
return startM
133+
134+
135+
#########################################################################################
136+
137+
list1 = SingleLinkedList()
138+
list2 = SingleLinkedList()
139+
140+
list1.create_list()
141+
list2.create_list()
142+
143+
list1.bubble_sort_exdata()
144+
list2.bubble_sort_exdata()
145+
146+
print("First List - "); list1.display_list()
147+
print("Second List - "); list2.display_list()
148+
149+
list3 = list1.merge1(list2)
150+
print("Merged List - "); list3.display_list()
151+
152+
print("First List - "); list1.display_list()
153+
print("Second List - "); list2.display_list()
154+
155+
list3 = list1.merge2(list2)
156+
print("Merged List - "); list3.display_list()
157+
print("First List - "); list1.display_list()
158+
print("Second List - "); list2.display_list()
159+

0 commit comments

Comments
 (0)