Skip to content

Commit dfdd781

Browse files
tianyizheng02github-actions
and
github-actions
authored
Fix mypy errors in circular_linked_list.py and swap_nodes.py (#9707)
* updating DIRECTORY.md * Fix mypy errors in circular_linked_list.py * Fix mypy errors in swap_nodes.py --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 3fd3497 commit dfdd781

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

DIRECTORY.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,8 @@
541541
* [Basic Maths](maths/basic_maths.py)
542542
* [Binary Exp Mod](maths/binary_exp_mod.py)
543543
* [Binary Exponentiation](maths/binary_exponentiation.py)
544-
* [Binary Exponentiation 2](maths/binary_exponentiation_2.py)
545544
* [Binary Exponentiation 3](maths/binary_exponentiation_3.py)
545+
* [Binary Multiplication](maths/binary_multiplication.py)
546546
* [Binomial Coefficient](maths/binomial_coefficient.py)
547547
* [Binomial Distribution](maths/binomial_distribution.py)
548548
* [Bisection](maths/bisection.py)
@@ -557,8 +557,7 @@
557557
* [Decimal Isolate](maths/decimal_isolate.py)
558558
* [Decimal To Fraction](maths/decimal_to_fraction.py)
559559
* [Dodecahedron](maths/dodecahedron.py)
560-
* [Double Factorial Iterative](maths/double_factorial_iterative.py)
561-
* [Double Factorial Recursive](maths/double_factorial_recursive.py)
560+
* [Double Factorial](maths/double_factorial.py)
562561
* [Dual Number Automatic Differentiation](maths/dual_number_automatic_differentiation.py)
563562
* [Entropy](maths/entropy.py)
564563
* [Euclidean Distance](maths/euclidean_distance.py)

data_structures/linked_list/circular_linked_list.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def __init__(self) -> None:
2020
"""
2121
Initialize an empty Circular Linked List.
2222
"""
23-
self.head = None # Reference to the head (first node)
24-
self.tail = None # Reference to the tail (last node)
23+
self.head: Node | None = None # Reference to the head (first node)
24+
self.tail: Node | None = None # Reference to the tail (last node)
2525

2626
def __iter__(self) -> Iterator[Any]:
2727
"""
@@ -30,7 +30,7 @@ def __iter__(self) -> Iterator[Any]:
3030
The data of each node in the linked list.
3131
"""
3232
node = self.head
33-
while self.head:
33+
while node:
3434
yield node.data
3535
node = node.next
3636
if node == self.head:
@@ -74,17 +74,20 @@ def insert_nth(self, index: int, data: Any) -> None:
7474
"""
7575
if index < 0 or index > len(self):
7676
raise IndexError("list index out of range.")
77-
new_node = Node(data)
77+
new_node: Node = Node(data)
7878
if self.head is None:
7979
new_node.next = new_node # First node points to itself
8080
self.tail = self.head = new_node
8181
elif index == 0: # Insert at the head
8282
new_node.next = self.head
83+
assert self.tail is not None # List is not empty, tail exists
8384
self.head = self.tail.next = new_node
8485
else:
85-
temp = self.head
86+
temp: Node | None = self.head
8687
for _ in range(index - 1):
88+
assert temp is not None
8789
temp = temp.next
90+
assert temp is not None
8891
new_node.next = temp.next
8992
temp.next = new_node
9093
if index == len(self) - 1: # Insert at the tail
@@ -120,16 +123,21 @@ def delete_nth(self, index: int = 0) -> Any:
120123
"""
121124
if not 0 <= index < len(self):
122125
raise IndexError("list index out of range.")
123-
delete_node = self.head
126+
127+
assert self.head is not None and self.tail is not None
128+
delete_node: Node = self.head
124129
if self.head == self.tail: # Just one node
125130
self.head = self.tail = None
126131
elif index == 0: # Delete head node
132+
assert self.tail.next is not None
127133
self.tail.next = self.tail.next.next
128134
self.head = self.head.next
129135
else:
130-
temp = self.head
136+
temp: Node | None = self.head
131137
for _ in range(index - 1):
138+
assert temp is not None
132139
temp = temp.next
140+
assert temp is not None and temp.next is not None
133141
delete_node = temp.next
134142
temp.next = temp.next.next
135143
if index == len(self) - 1: # Delete at tail

data_structures/linked_list/swap_nodes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ def __init__(self, data: Any) -> None:
1111
1212
"""
1313
self.data = data
14-
self.next = None # Reference to the next node
14+
self.next: Node | None = None # Reference to the next node
1515

1616

1717
class LinkedList:
1818
def __init__(self) -> None:
1919
"""
2020
Initialize an empty Linked List.
2121
"""
22-
self.head = None # Reference to the head (first node)
22+
self.head: Node | None = None # Reference to the head (first node)
2323

2424
def print_list(self):
2525
"""

0 commit comments

Comments
 (0)