forked from super30admin/PreCourse-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_3.py
More file actions
53 lines (41 loc) · 1.15 KB
/
Exercise_3.py
File metadata and controls
53 lines (41 loc) · 1.15 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
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):
new_node = ListNode(data)
if self.head is None:
self.head = new_node
return
current = self.head
while current.next:
current = curren
def find(self, key):
current = self.head
while current:
if current.data == key:
return current
current = current.next
return None
def remove(self, key):
current = self.head
prev = None
while current:
if current.data == key:
if prev:
prev.next = current.next
else:
self.head = current.next
return
prev = current
current = current.next