-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.py
More file actions
135 lines (100 loc) · 3.86 KB
/
Copy pathlinked_list.py
File metadata and controls
135 lines (100 loc) · 3.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
"""
A class representing a singly linked list.
Attributes:
- head: Points to the first node in the linked list.
Methods:
- __init__(): Initializes an empty linked list with a null head.
- insert(value): Inserts a new node with the given value at the head of the list.
- includes(value): Checks if a node with the specified value exists in the linked list.
- __str__(): Returns a string representation of the linked list in the format "{ value1 } -> { value2 } -> ... -> NULL".
- append(value): Adds a new node with the given value to the end of the list.
- insert_before(value, new_value): Inserts a new node with the specified new_value before the node with the given value.
- insert_after(value, new_value): Inserts a new node with the specified new_value after the node with the given value.
- kth_from_end(k): Returns the node's value that is k places from the tail of the linked list.
"""
def __init__(self):
self.head = None
def insert (self, value):
# adds new node to beginning of list
new_node = Node(value)
new_node.next = self.head
self.head = new_node
def includes(self, value):
current = self.head
while current:
if current.value == value:
return True
current = current.next
return False
def __str__(self):
result = ""
current = self.head
while current:
result += f"{{ {current.value} }} -> "
current = current.next
result += "NULL"
return result
# linked_list = LinkedList()
# linked_list.insert(3)
# linked_list.insert(7)
# linked_list.insert(10)
# print(linked_list)
def append(self, value):
# adds new node to end of 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 insert_before(self, value, new_value):
if self.head is None:
raise TargetError("Empty list, unable to insert before")
if self.head is not None and self.head.value == value:
self.insert(new_value)
return
current = self.head
while current.next is not None and current.next.value != value:
current = current.next
if current.next is None:
raise TargetError(f"Value {value} is not in linked list")
new_node = Node(new_value)
new_node.next = current.next
current.next= new_node
def insert_after(self, value, new_value):
if self.head is None:
raise TargetError("Empty list unable to insert after")
current = self.head
while current is not None and current.value != value:
current = current.next
if current is None:
raise TargetError(f"Value {value} is not in linked list")
new_node = Node(new_value)
new_node.next = current.next
current.next = new_node
def kth_from_end(self, k):
if self.head is None:
raise TargetError("Empty list unable to find kth from end")
if k < 0:
raise TargetError("k can't be negative")
length =0
current=self.head
while current is not None:
length+=1
current=current.next
if k >= length:
raise TargetError("k is great than or equal to length of list")
current = self.head
for _ in range(length-k-1):
current=current.next
return current.value
class TargetError (Exception):
def __init__(self, message):
self.message = message