Repository: TheAlgorithms/Python

Issue:
Failing precommit.ci due to error in circular_linked_list.py file
### Repository commit

#9668

### Python version (python --version)

3.10.7

### Dependencies version (pip freeze)

absl-py==2.0.0
anyio==3.7.0
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
arrow==1.2.3
asttokens==2.2.1
attrs==23.1.0
audioread==3.0.0
backcall==0.2.0
beautifulsoup4==4.12.2
black==23.9.1
bleach==6.0.0
certifi==2023.7.22
cffi==1.15.1
cfgv==3.4.0
charset-normalizer==3.2.0
click==8.1.7
colorama==0.4.6
comm==0.1.3
contourpy==1.0.7
cycler==0.11.0
debugpy==1.6.7
decorator==5.1.1
defusedxml==0.7.1
distlib==0.3.7
executing==1.2.0
fastjsonschema==2.17.1
filelock==3.12.4
flatbuffers==23.5.26
fonttools==4.39.3
fqdn==1.5.1
identify==2.5.30
idna==3.4
ipykernel==6.23.2
ipython==8.14.0
ipython-genutils==0.2.0
isoduration==20.11.0
jedi==0.18.2
Jinja2==3.1.2
joblib==1.2.0
jsonpointer==2.4
jsonschema==4.17.3
jupyter-events==0.6.3
jupyter_client==8.2.0
jupyter_core==5.3.1
jupyter_server==2.6.0
jupyter_server_terminals==0.4.4
jupyterlab-pygments==0.2.2
keras==2.13.1
kiwisolver==1.4.4
lazy_loader==0.3
librosa==0.10.1
llvmlite==0.40.1
MarkupSafe==2.1.3
matplotlib==3.7.1
matplotlib-inline==0.1.6
mediapipe==0.10.5
mistune==3.0.1
msgpack==1.0.5
mypy-extensions==1.0.0
mysql-connector-python==8.1.0
nbclassic==1.0.0
nbclient==0.8.0
nbconvert==7.6.0
nbformat==5.9.0
nest-asyncio==1.5.6
nodeenv==1.8.0
noisereduce==2.0.1
notebook==6.5.4
notebook_shim==0.2.3
numba==0.57.1
numpy==1.24.3
opencv-contrib-python==4.8.1.78
opencv-python==4.7.0.72
overrides==7.3.1
packaging==23.1
pandas==2.0.2
pandocfilters==1.5.0
parso==0.8.3
pathspec==0.11.2
pickleshare==0.7.5
Pillow==9.5.0
platformdirs==3.11.0
pooch==1.7.0
pre-commit==3.4.0
prometheus-client==0.17.0
prompt-toolkit==3.0.38
protobuf==3.20.3
psutil==5.9.5
pure-eval==0.2.2
PyAudio==0.2.13
pycparser==2.21
pycryptodome==3.18.0
pygame==2.5.2
Pygments==2.15.1
pyparsing==3.0.9
pyrsistent==0.19.3
python-dateutil==2.8.2
python-json-logger==2.0.7
pytz==2023.3
pywin32==306
pywinpty==2.0.10
PyYAML==6.0
pyzmq==25.1.0
requests==2.31.0
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
ruff==0.0.292
scikit-learn==1.2.2
scipy==1.10.1
seaborn==0.12.2
Send2Trash==1.8.2
six==1.16.0
sklearn==0.0.post5
sniffio==1.3.0
sounddevice==0.4.6
soundfile==0.12.1
soupsieve==2.4.1
soxr==0.3.6
stack-data==0.6.2
terminado==0.17.1
threadpoolctl==3.1.0
tinycss2==1.2.1
tornado==6.3.2
tqdm==4.66.1
traitlets==5.9.0
typing_extensions==4.7.1
tzdata==2023.3
uri-template==1.2.0
urllib3==2.0.4
virtualenv==20.24.5
Wave==0.0.2
wcwidth==0.2.6
webcolors==1.13
webencodings==0.5.1
websocket-client==1.6.0

### Expected behavior

the precommit should get pass

### Actual behavior

fainling in precommit
![Screenshot (62)](https://github.com/TheAlgorithms/Python/assets/99018821/e443c933-c3df-40c7-9d64-92d69f644bed)



Discussion:

Resolving patch:
diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py
--- a/data_structures/linked_list/circular_linked_list.py
+++ b/data_structures/linked_list/circular_linked_list.py
@@ -20,8 +20,8 @@ def __init__(self) -> None:
         """
         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 @@ def __iter__(self) -> Iterator[Any]:
             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 @@ def insert_nth(self, index: int, data: Any) -> None:
         """
         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 @@ def delete_nth(self, index: int = 0) -> Any:
         """
         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
diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py
--- a/data_structures/linked_list/swap_nodes.py
+++ b/data_structures/linked_list/swap_nodes.py
@@ -11,7 +11,7 @@ def __init__(self, data: Any) -> None:
 
         """
         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 @@ def __init__(self) -> None:
         """
         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):
         """


Test patch: