forked from PLPAfrica/Python-Hack1---May-Cohort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion 3.py
More file actions
35 lines (29 loc) · 948 Bytes
/
Question 3.py
File metadata and controls
35 lines (29 loc) · 948 Bytes
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
# Question 3
class Node:
def __init__(self, value: int):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, value: int):
"""Append a new node with the given value to the end of the list."""
new_node = Node(value)
if not self.head:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
def find_max(self) -> int:
"""Find the maximum value in the linked list."""
if not self.head:
raise ValueError("The linked list is empty")
current = self.head
max_value = current.value
while current:
if current.value > max_value:
max_value = current.value
current = current.next
return max_value