@@ -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
0 commit comments