mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 08:17:01 +00:00
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>
This commit is contained in:
parent
3fd3497f15
commit
dfdd78135d
|
@ -541,8 +541,8 @@
|
|||
* [Basic Maths](maths/basic_maths.py)
|
||||
* [Binary Exp Mod](maths/binary_exp_mod.py)
|
||||
* [Binary Exponentiation](maths/binary_exponentiation.py)
|
||||
* [Binary Exponentiation 2](maths/binary_exponentiation_2.py)
|
||||
* [Binary Exponentiation 3](maths/binary_exponentiation_3.py)
|
||||
* [Binary Multiplication](maths/binary_multiplication.py)
|
||||
* [Binomial Coefficient](maths/binomial_coefficient.py)
|
||||
* [Binomial Distribution](maths/binomial_distribution.py)
|
||||
* [Bisection](maths/bisection.py)
|
||||
|
@ -557,8 +557,7 @@
|
|||
* [Decimal Isolate](maths/decimal_isolate.py)
|
||||
* [Decimal To Fraction](maths/decimal_to_fraction.py)
|
||||
* [Dodecahedron](maths/dodecahedron.py)
|
||||
* [Double Factorial Iterative](maths/double_factorial_iterative.py)
|
||||
* [Double Factorial Recursive](maths/double_factorial_recursive.py)
|
||||
* [Double Factorial](maths/double_factorial.py)
|
||||
* [Dual Number Automatic Differentiation](maths/dual_number_automatic_differentiation.py)
|
||||
* [Entropy](maths/entropy.py)
|
||||
* [Euclidean Distance](maths/euclidean_distance.py)
|
||||
|
|
|
@ -20,8 +20,8 @@ class CircularLinkedList:
|
|||
"""
|
||||
Initialize an empty Circular Linked List.
|
||||
"""
|
||||
self.head = None # Reference to the head (first node)
|
||||
self.tail = None # Reference to the tail (last node)
|
||||
self.head: Node | None = None # Reference to the head (first node)
|
||||
self.tail: Node | None = None # Reference to the tail (last node)
|
||||
|
||||
def __iter__(self) -> Iterator[Any]:
|
||||
"""
|
||||
|
@ -30,7 +30,7 @@ class CircularLinkedList:
|
|||
The data of each node in the linked list.
|
||||
"""
|
||||
node = self.head
|
||||
while self.head:
|
||||
while node:
|
||||
yield node.data
|
||||
node = node.next
|
||||
if node == self.head:
|
||||
|
@ -74,17 +74,20 @@ class CircularLinkedList:
|
|||
"""
|
||||
if index < 0 or index > len(self):
|
||||
raise IndexError("list index out of range.")
|
||||
new_node = Node(data)
|
||||
new_node: Node = Node(data)
|
||||
if self.head is None:
|
||||
new_node.next = new_node # First node points to itself
|
||||
self.tail = self.head = new_node
|
||||
elif index == 0: # Insert at the head
|
||||
new_node.next = self.head
|
||||
assert self.tail is not None # List is not empty, tail exists
|
||||
self.head = self.tail.next = new_node
|
||||
else:
|
||||
temp = self.head
|
||||
temp: Node | None = self.head
|
||||
for _ in range(index - 1):
|
||||
assert temp is not None
|
||||
temp = temp.next
|
||||
assert temp is not None
|
||||
new_node.next = temp.next
|
||||
temp.next = new_node
|
||||
if index == len(self) - 1: # Insert at the tail
|
||||
|
@ -120,16 +123,21 @@ class CircularLinkedList:
|
|||
"""
|
||||
if not 0 <= index < len(self):
|
||||
raise IndexError("list index out of range.")
|
||||
delete_node = self.head
|
||||
|
||||
assert self.head is not None and self.tail is not None
|
||||
delete_node: Node = self.head
|
||||
if self.head == self.tail: # Just one node
|
||||
self.head = self.tail = None
|
||||
elif index == 0: # Delete head node
|
||||
assert self.tail.next is not None
|
||||
self.tail.next = self.tail.next.next
|
||||
self.head = self.head.next
|
||||
else:
|
||||
temp = self.head
|
||||
temp: Node | None = self.head
|
||||
for _ in range(index - 1):
|
||||
assert temp is not None
|
||||
temp = temp.next
|
||||
assert temp is not None and temp.next is not None
|
||||
delete_node = temp.next
|
||||
temp.next = temp.next.next
|
||||
if index == len(self) - 1: # Delete at tail
|
||||
|
|
|
@ -11,7 +11,7 @@ class Node:
|
|||
|
||||
"""
|
||||
self.data = data
|
||||
self.next = None # Reference to the next node
|
||||
self.next: Node | None = None # Reference to the next node
|
||||
|
||||
|
||||
class LinkedList:
|
||||
|
@ -19,7 +19,7 @@ class LinkedList:
|
|||
"""
|
||||
Initialize an empty Linked List.
|
||||
"""
|
||||
self.head = None # Reference to the head (first node)
|
||||
self.head: Node | None = None # Reference to the head (first node)
|
||||
|
||||
def print_list(self):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user