-
Notifications
You must be signed in to change notification settings - Fork 0
/
one.py
154 lines (137 loc) · 4.14 KB
/
one.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
class node:
def __init__(self, data=None):
self.data = data
self.next = None
class linked_list:
def __init__(self):
self.head = node()
def append(self,data):
temp = node(data)
current = self.head
while current.next != None:
current = current.next
current.next = temp
def insertUpFront(self,data):
temp = self.head.next
new = node(data)
self.head.next = new
new.next = temp
def delete(self,data):
start = self.head.next
temp = self.head
while start is not None:
if start.data == data:
return temp.next = start.next
else:
start = start.next
temp = temp.next
def findNode(self,data):
temp = self.head.next
while temp is not None:
if temp.data == data:
return temp
temp = temp.next
return "The Node is not found in the list"
def remove(self):
temp = self.head
while temp.next.next != None:
temp = temp.next
temp.next = None
def length(self):
current = self.head
size = 0
while current.next != None:
size += 1
current = current.next
return size
def display(self):
temp = []
current = self.head
while current.next != None:
current = current.next
temp.append(current.data)
print (temp)
def getIndex(self,index):
if index >= self.length():
return "The index is out of range"
temp = self.head.next
pos = 0
while pos != index:
temp = temp.next
pos += 1
return temp
def getMiddle(self):
if self.length() % 2 == 0:
ans = self.getIndex(self.length() / 2)
else: ans = self.getIndex(((self.length() + 1) / 2) - 1)
return ans
def getMiddleNode(self):
slow = fast = self.head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
def getMiddleList(self):
middle_list = []
if self.length() % 2 == 0:
temp = self.getIndex(self.length() / 2)
while temp.next != None:
middle_list.append(temp.data)
return middle_list
temp = self.getIndex(((self.length() + 1) / 2) - 1)
while temp != None:
middle_list.append(temp.data)
temp = temp.next
return middle_list
def reverseList(self):
start = self.head
rev = linked_list()
while start.next != None:
temp = self.head.next
while temp.next != None:
temp = temp.next
rev.append(temp.data)
self.remove()
return rev
def merge_Slist(self,l2):
p1 = self.head.next
p2 = l2.head.next
final = linked_list()
while p1 or p2:
if p1 == None:
final.append(p2.data)
p2 = p2.next
elif p2 == None:
final.append(p1.data)
p1 = p1.next
elif p1.data < p2.data:
final.append(p1.data)
p1 = p1.next
elif p1.data == p2.data:
final.append(p1.data)
p1 = p1.next
p2 = p2.next
else:
final.append(p2.data)
p2 = p2.next
final.display()
def remove_duplicates(self):
start = self.head.next
temp = self.head.next.next
while temp != None:
if start.data == temp.data:
start.next = temp.next
temp = temp.next
else:
start = start.next
temp = temp.next
self.display()
def hasCycle(self):
start = self.head.next
nodes = {}
while start.next != None:
if start in nodes:
return True
nodes[start] = start.data
start = start.next
return False