-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.py
More file actions
44 lines (35 loc) · 1.02 KB
/
linked_list.py
File metadata and controls
44 lines (35 loc) · 1.02 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
from node import Node
class LinkedList():
def __init__(self):
self.__root = None
self.__length = 0
def count(self):
return self.__length
def insert_first(self, elem):
newNode = Node(elem, self.__root)
self.__root = newNode
self.__length += 1
def delete_first(self):
result = self.__root.value
self.__root = self.__root.link
self.__length -= 1
return result
def insert_after(self, node, e):
newNode = Node(e, node.link)
node.link = newNode
self.__length += 1
def delete_after(self, node):
node.link = node.link.link
self.__length -= 1
def print_linkedlist(self):
current = self.__root
while current:
print(current.value)
current = current.link
def find(self, val):
current = self.__root
while current:
if current.value == val:
return current
current = current.link
return None