-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcll.py
More file actions
225 lines (173 loc) · 4.93 KB
/
cll.py
File metadata and controls
225 lines (173 loc) · 4.93 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
Circular Linked Lists
"""
class SinglyLinkedList:
# based around the concept of Nodes
# creates a chain of Nodes by connecting them with one another
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
# handle for if the list is empty
if not self.head:
self.head = new_node
return
# handle for if the list is not empty
cur_node = self.head
while cur_node.next:
cur_node = cur_node.next
cur_node.next = new_node
def print_list(self):
# A -> B -> C -> None
# self.head = "A" -> self.head -> "B" -> self.head -> "C"... -> self.head -> None
cur_node = self.head
while cur_node:
print(cur_node.data)
cur_node = cur_node.next
class Node:
def __init__(self, data):
self.data = data
self.next = None
# list keeps circling around on itself
# A > B > C > D > A > B > C...
class CircularLinkedList:
def __init__(self):
self.head = None
def __len__(self):
if not self.head:
return 0
cur = self.head
count = 1
while cur.next != self.head:
cur = cur.next
count += 1
return count
def print_list(self):
cur = self.head
while cur:
print(cur.data)
cur = cur.next
if cur == self.head:
break
def append(self, data):
new_node = Node(data)
# if the list is empty
if not self.head:
self.head = new_node
self.head.next = self.head
# if list is not empty
cur = self.head
while cur:
if cur.next == self.head:
break
cur = cur.next
cur.next = new_node
new_node.next = self.head
def prepend(self, data):
if not self.head:
self.head = Node(data)
self.head.next = self.head
return
new = Node(data)
# A -> B -> C -> D -> A
orig_head = self.head
new.next = orig_head
self.head = new
last = orig_head
while last.next != orig_head:
last = last.next
last.next = self.head
def delete(self, data):
if self.head.data == data:
cur = self.head
last = self.head
while last.next != self.head:
last = last.next
last.next = cur.next
self.head = last.next
return
prev = None
cur = self.head
while cur.next != self.head and cur.data != data:
prev = cur
cur = cur.next
if cur.data == data:
prev.next = cur.next
cur = None
def insert(self, prev_data, data):
new_node = Node(data)
if not self.head:
self.head = new_node
new_node.next = new_node
return
prev = None
cur = self.head
while cur:
prev = cur
cur = cur.next
if prev.data == prev_data:
prev.next = new_node
new_node.next = cur
return
if cur == self.head:
print("prev_data passed doesn't exist in the list")
return
def remove_fibonacci_nums(self): # 0 1 1 2 3 5 8 13 21 34 55 89
# find the largest number
cur = self.head
largest_num = cur.data
while cur:
cur = cur.next
if cur == self.head:
break
if cur.data > largest_num:
largest_num = cur.data
# create fib_array
fib_array = [0, 1]
num = None
while num < largest_num:
last = fib_array[-1]
second_to_last = fib_array[-2]
num = last + second_to_last
fib_array.append(num)
# print(fib_array)
# remove fib nodes
prev = None
cur2 = self.head
old_head = self.head
while cur2:
if cur2.data in fib_array:
if cur2 == self.head:
self.head = self.head.next
cur2 = self.head
else:
nxt = cur2.next
prev.next = nxt
cur2 = nxt
else:
prev = cur2
cur2 = cur2.next
if cur2 == old_head:
prev.next = self.head
break
def is_circular_linked_list(ll):
cur = ll.head
while cur.next:
cur = cur.next
if cur == ll.head:
return True
return False
cll = CircularLinkedList()
cll.append(1)
cll.append(2)
cll.append(3)
cll.append(4)
# cll.print_list()
sll = SinglyLinkedList()
sll.append(1)
sll.append(2)
sll.append(3)
sll.append(4)
# sll.print_list()
print(is_circular_linked_list(cll))
print(is_circular_linked_list(sll))