forked from super30admin/PreCourse-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise_3.py
70 lines (63 loc) · 1.73 KB
/
Exercise_3.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
class ListNode:
"""
A node in a singly-linked list.
"""
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class SinglyLinkedList:
def __init__(self):
"""
Create a new singly-linked list.
Takes O(1) time.
"""
self.head = None
def append(self, data):
"""
Insert a new element at the end of the list.
Takes O(n) time.
"""
new_node = ListNode(data)
if self.head is None:
self.head = new_node
return
end_node = self.head
while end_node.next:
end_node = end_node.next
end_node.next = new_node
def find(self, key):
"""
Search for the first element with `data` matching
`key`. Return the element or `None` if not found.
Takes O(n) time.
"""
node = self.head
while node.next:
if node.data == key:
return node
else:
return None
def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
#empty list check
if self.head is None:
return
# remove node at start
if self.head.data == key:
temp_node = self.head
self.head = temp_node.next
temp_node.next = None
# remove node at any other place
prev_node = self.head
node = self.head.next
while node.next:
if node.data == key:
break
else:
prev_node = prev_node.next
node = node.next
prev_node.next = node.next
node.next = None