-
Notifications
You must be signed in to change notification settings - Fork 0
/
merger_2_lists.py
162 lines (120 loc) · 3.32 KB
/
merger_2_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
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
class Node:
def __init__(self):
self.data=None
self.next=None
def set_data(self,data):
self.data=data
def get_data(self):
return self.data
def set_next(self,next):
self.next=next
def get_next(self):
return self.next
def has_next(self):
return self.next!=None
class merge_2_lists:
def __init__(self,head=None):
self.head=head
self.length=0
def list_length(self):
curr=self.head
count=0
while curr!=None:
count+=1
curr=curr.get_next()
return count
def insert_at_begin(self,data):
newnode=Node()
newnode.data=data
if self.length==0:
self.head=newnode
else:
newnode.set_next(self.head)
self.head=newnode
self.length+=1
return newnode.data
def print_nodes(self):
cur=Node()
cur=self.head
while cur!=None:
print(cur.data,"-->",end="")
cur=cur.get_next()
print()
def merge_lists(self,l2):
l2n=l2.head
cur=self.head
while(cur!=None and l2n!=None):
cn=cur.next
l2n_next=l2n.next
l2n.next=cn
cur.next=l2n
cur=cn
l2n=l2n_next
l2.head=l2n
def merge2(self,l2):
temp=self.head
while temp.next!=None:
temp=temp.next
temp.next=l2.head
def partition(self):
temp=self.head
k=5
l3=Node()
l3=None
l5=Node()
l4=Node()
l4=None
l5=None
while temp!=None:
print(temp.data)
if temp.data < k:
if l3==None:
l3=temp
print(l3.data)
l3=l3.next
else:
l3.next=temp
print(l3.data)
l3=l3.next
elif temp.data > k:
if l5==None:
l5=temp
else:
l5.next=temp
l5=l5.next
elif k==temp.data:
if l4==None:
l4=temp
else:
l4.next=temp
l4=l4.next
temp=temp.next
'''l5.next=None
l3.next=l4
l4.next=l5'''
print()
while l3!=None:
print(l3.data,"->",end="")
l3=l3.next
print()
while l4!=None:
print(l4.data,"->",end="")
l4=l4.next
print()
while l5!=None:
print(l5.data,"->",end="")
l5=l5.next
l1=merge_2_lists()
l2=merge_2_lists()
l1.insert_at_begin(1)
l1.insert_at_begin(2)
l1.insert_at_begin(10)
l1.insert_at_begin(5)
l1.insert_at_begin(8)
l1.insert_at_begin(5)
l1.insert_at_begin(3)
l1.print_nodes()
l1.partition()
'''l1.merge_lists(l2)
l1.print_nodes()
'''