Skip to content

Commit da344c8

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 2b32791 commit da344c8

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

data_structures/linked_list/circular_doubly_linked_list.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Node:
1010
"""
1111
A node in a circular doubly linked list.
1212
"""
13+
1314
data: Any
1415
next_node: Node | None = None
1516
prev_node: Node | None = None
@@ -24,6 +25,7 @@ class CircularDoublyLinkedList:
2425
- The last node's next points to the first node
2526
- The first node's previous points to the last node
2627
"""
28+
2729
head: Node | None = None # Reference to the head (first node)
2830
tail: Node | None = None # Reference to the tail (last node)
2931

@@ -35,7 +37,7 @@ def __iter__(self) -> Iterator[Any]:
3537
"""
3638
if self.head is None:
3739
return
38-
40+
3941
node = self.head
4042
while True:
4143
yield node.data
@@ -81,9 +83,9 @@ def insert_nth(self, index: int, data: Any) -> None:
8183
"""
8284
if index < 0 or index > len(self):
8385
raise IndexError("list index out of range.")
84-
86+
8587
new_node: Node = Node(data)
86-
88+
8789
if self.head is None:
8890
# First node - points to itself in both directions
8991
new_node.next_node = new_node
@@ -111,12 +113,12 @@ def insert_nth(self, index: int, data: Any) -> None:
111113
for _ in range(index):
112114
assert temp is not None
113115
temp = temp.next_node
114-
116+
115117
assert temp is not None
116118
# Insert before temp
117119
prev_node = temp.prev_node
118120
assert prev_node is not None
119-
121+
120122
new_node.next_node = temp
121123
new_node.prev_node = prev_node
122124
temp.prev_node = new_node
@@ -155,7 +157,7 @@ def delete_nth(self, index: int = 0) -> Any:
155157

156158
assert self.head is not None
157159
assert self.tail is not None
158-
160+
159161
if self.head == self.tail:
160162
# Only one node
161163
delete_node = self.head
@@ -173,20 +175,20 @@ def delete_nth(self, index: int = 0) -> Any:
173175
for _ in range(index):
174176
assert delete_node is not None
175177
delete_node = delete_node.next_node
176-
178+
177179
assert delete_node is not None
178180
prev_node = delete_node.prev_node
179181
next_node = delete_node.next_node
180-
182+
181183
assert prev_node is not None
182184
assert next_node is not None
183-
185+
184186
prev_node.next_node = next_node
185187
next_node.prev_node = prev_node
186-
188+
187189
if delete_node == self.tail:
188190
self.tail = prev_node
189-
191+
190192
return delete_node.data
191193

192194
def is_empty(self) -> bool:
@@ -213,7 +215,7 @@ def traverse_backward(self) -> list[Any]:
213215
"""
214216
if self.tail is None:
215217
return []
216-
218+
217219
result = []
218220
node = self.tail
219221
while True:
@@ -292,8 +294,14 @@ def test_circular_doubly_linked_list() -> None:
292294

293295
# Test circular property
294296
if circular_doubly_linked_list.head and circular_doubly_linked_list.tail:
295-
assert circular_doubly_linked_list.head.prev_node == circular_doubly_linked_list.tail
296-
assert circular_doubly_linked_list.tail.next_node == circular_doubly_linked_list.head
297+
assert (
298+
circular_doubly_linked_list.head.prev_node
299+
== circular_doubly_linked_list.tail
300+
)
301+
assert (
302+
circular_doubly_linked_list.tail.next_node
303+
== circular_doubly_linked_list.head
304+
)
297305

298306

299307
if __name__ == "__main__":

0 commit comments

Comments
 (0)