-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathExercise_3.py
More file actions
74 lines (67 loc) · 1.86 KB
/
Copy pathExercise_3.py
File metadata and controls
74 lines (67 loc) · 1.86 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
# Time and Space Complexity
# Overall Space Complexity is O(N) where N is total number of elements in the list
# append function takes O(N) time complexity and O(1) space complexity
# find function takes O(N) time complexity and O(1) space complexity
# remove function takes O(N) time complexity and O(1) space complexity
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.
"""
newNode=ListNode(data)
if self.head==None:
self.head=newNode
else:
a=self.head
while a.next!=None:
a=a.next
a.next=newNode
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.
"""
a=self.head
while a!=None:
if a.data==key:
return a
a=a.next
return None
def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""
a=self.head
if self.head==None:
return None
if self.head.data==key:
self.head=self.head.next
return None
while a.next!=None:
if a.next.data==key:
a.next=a.next.next
return None
a=a.next
ll = SinglyLinkedList()
ll.append(10)
ll.append(20)
ll.append(30)
found = ll.find(20)
print(found.data)
ll.remove(20)