-
Notifications
You must be signed in to change notification settings - Fork 43
/
linkedList.py
316 lines (286 loc) · 7.76 KB
/
linkedList.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#---------------------------------------------------------------
# LINKED LIST
#---------------------------------------------------------------
# V0
# https://github.com/youngyangyang04/leetcode-master/blob/master/problems/%E9%93%BE%E8%A1%A8%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.md
# V1
class Node:
"""
# constructor
# A single node of a singly linked list
"""
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class LinkedList:
"""
# A Linked List class with a single head node
"""
def __init__(self):
self.head = None
def get_LL_length(self):
"""
# get list length method for the linked list
i.e.
before : 1 -> 2 -> 3
after : 3
"""
current = self.head
length = 0
while current:
current = current.next
length += 1
return length
def get_LL_tail(self):
"""
# get list tail method for the linked list
i.e.
before : a -> b -> c
after : c
"""
current = self.head
while current:
current = current.next
return current
def printLL(self):
"""
# print method for the linked list
i.e.
before : 1 -> 2 -> 3
after : 1 2 3
"""
current = self.head
while current:
print (current.data)
current = current.next
def append(self, data):
"""
# append method that append a new item at the end of the linkedlist
i.e.
before : 1 -> 2 -> 3
after : 1 -> 2 -> 3 -> 4
"""
newNode = Node(data)
if self.head:
current = self.head
while current.next:
current = current.next
current.next = newNode
else:
self.head = newNode
def prepend(self, data):
"""
# append method that append a new item at the head of the linkedlist
i.e.
before : 1 -> 2 -> 3
after : 0 -> 1 -> 2 -> 3
"""
newNode = Node(data)
if self.head:
current = self.head
self.head = newNode
newNode.next = current
current = current.next
else:
self.head = newNode
def insert(self, idx, data):
"""
# append method that append a new item within the linkedlist
i.e.
before : 1 -> 2 -> 3
insert(1, 2.5)
after : 1 -> 2 -> 2.5 -> 3
before : 1 -> 2 -> 3
insert(0, 0)
after : 0 -> 1 -> 2 -> 3
before : 1 -> 2 -> 3
insert(2, 4)
after : 1 -> 2 -> 3 -> 4
"""
current = self.head
ll_length = self.get_LL_length()
if idx < 0 or idx > self.get_LL_length():
print ("idx out of linkedlist range, idx : {}".format(idx))
return
elif idx == 0:
self.prepend(data)
elif idx == ll_length:
self.append(data)
else:
newNode = Node(data)
current = self.head
cur_idx = 0
while cur_idx < idx-1:
current = current.next
cur_idx += 1
newNode.next = current.next
current.next = newNode
def remove(self, idx):
"""
# remove method for the linked list
i.e.
before : 1 -> 2 -> 3
remove(1)
after : 1 -> 3
before : 1 -> 2 -> 3
remove(2)
after : 1 -> 2
before : 1 -> 2 -> 3
remove(0)
after : 2 -> 3
"""
if idx < 0 or idx > self.get_LL_length():
print ("idx out of linkedlist range, idx : {}".format(idx))
return
elif idx == 0:
current = self.head
self.head = current.next
elif idx == self.get_LL_length():
current = self.head
cur_idx = 0
while cur_idx < idx -1:
current = current.next
cur_idx += 1
current.next = None
else:
current = self.head
cur_idx = 0
while cur_idx < idx - 1:
current = current.next
cur_idx += 1
next_ = current.next.next
current.next = next_
current = next_
def reverse(self):
"""
# reverse method for the linked list
# https://www.geeksforgeeks.org/python-program-for-reverse-a-linked-list/
i.e.
before : 1 -> 2 -> 3
after : 3 -> 2 -> 1
"""
prev = None
current = self.head
while(current is not None):
next_ = current.next
current.next = prev
prev = current
current = next_
self.head = prev
# LL = LinkedList()
# LL.append(3)
# LL.append(4)
# LL.append(5)
# LL.prepend(2)
# LL.append(6)
# LL.prepend(1)
# print ("print LinkedList : ")
# LL.printLL()
# LL.insert(2, 1.5)
# print ("print LinkedList : ")
# LL.printLL()
# LL.insert(7, 100)
# print ("print LinkedList : ")
# LL.printLL()
# LL.remove(0)
# print ("print LinkedList : ")
# LL.printLL()
# LL.remove(0)
# print ("print LinkedList : ")
# LL.printLL()
# LL.reverse()
# print ("print LinkedList : ")
# LL.printLL()
# V1'
# http://zhaochj.github.io/2016/05/12/2016-05-12-%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-%E9%93%BE%E8%A1%A8/
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def is_empty(self):
return self.head is None
def append(self, data):
node = Node(data)
if self.head is None:
self.head = node
self.tail = node
else:
self.tail.next = node
self.tail = node
def iter(self):
if not self.head:
return
cur = self.head
yield cur.data
while cur.__next__:
cur = cur.__next__
yield cur.data
def insert(self, idx, value):
cur = self.head
cur_idx = 0
if cur is None: # check is it's null LinkedList
raise Exception('The list is an empty list')
while cur_idx < idx-1: # go through LinkedList
cur = cur.__next__
if cur is None: # check if it's the last element
raise Exception('list length less than index')
cur_idx += 1
node = Node(value)
node.next = cur.__next__
cur.next = node
if node.__next__ is None:
self.tail = node
def remove(self, idx):
cur = self.head
cur_idx = 0
if self.head is None: # if it's null LinkedList
raise Exception('The list is an empty list')
while cur_idx < idx-1:
cur = cur.__next__
if cur is None:
raise Exception('list length less than index')
cur_idx += 1
if idx == 0: # when delete the first node
self.head = cur.__next__
cur = cur.__next__
return
if self.head is self.tail: # when there is only 1 node in the LinkedList
self.head = None
self.tail = None
return
cur.next = cur.next.__next__
if cur.__next__ is None: # when delete the last node in the LinkedList
self.tail = cur
def size(self):
current = self.head
count = 0
if current is None:
return 'The list is an empty list'
while current is not None:
count += 1
current = current.__next__
return count
def search(self, item):
current = self.head
found = False
while current is not None and not found:
if current.data == item:
found = True
else:
current = current.__next__
return found
# link_list = LinkedList()
# for i in range(150):
# link_list.append(i)
# # print(link_list.is_empty())
# # link_list.insert(10, 30)
# # link_list.remove(0)
# for node in link_list.iter():
# print(('node is {0}'.format(node)))
# print((link_list.size()))
# # print(link_list.search(20))
# V2