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
82 lines (67 loc) · 2.14 KB
/
Exercise_3.py
File metadata and controls
82 lines (67 loc) · 2.14 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
75
76
77
78
79
80
81
82
# Time Complexity : O(1)- append, others - O(n)
# Space Complexity :O(n)
# Did this code successfully run on Leetcode :yes
# Any problem you faced while coding this :yes
# Your code here along with comments explaining your approach
# The code creates a singly linked list where each node stores a value and a pointer to the next node.
# Insertion takes O(n) because it traverses to the last node before appending, and printing also takes O(n) as it visits all nodes.
# Space complexity is O(n) since each inserted element allocates a new node in memory.
class ListNode:
"""
A node in a singly-linked list.
"""
def __init__(self, data=None, next=None):
self.data = data
self.next = None
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 list is empty
if self.head is None:
self.head = new_node
return
# Traverse to the end
current = self.head
while current.next:
current = current.next
current.next = new_node
def find(self, key):
"""
Search for the first element with data == key.
Return the node or None.
Takes O(n) time.
"""
current = self.head
while current:
if current.data == key:
return current
current = current.next
return None
def remove(self, key):
"""
Remove the first occurrence of key in the list.
Takes O(n) time.
"""
current = self.head
previous = None
if not current:
return
if current.data == key:
self.head = current.next
return
while current:
if current.data == key:
previous.next = current.next
return
previous = current
current = current.next