-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.py
52 lines (45 loc) · 1.24 KB
/
linked_list.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
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert(self, value):
new_item = Node(value)
new_item.next = self.head
self.head = new_item
def is_empty(self):
return self.head is None
def size(self):
current = self.head
count = 0
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.value == item:
found = True
else:
current = current.next
return found
def remove(self, item):
current = self.head
previous = None
found = False
while not found:
if current.value == item:
found = True
elif current.next is None:
return
else:
previous = current
current = current.next
if previous is None:
self.head = current.next
else:
previous.next = current.next