Skip to content

Commit 0924dcb

Browse files
authored
Update circular_linked_list.py
Summary of Fixes:insert_nth Method:Corrected logic to update tail when a new node is inserted at the tail.Ensured correct handling of node linking when inserting at various positions.delete_nth Method:Clarified the logic for resetting head and tail when deleting the only node in the list.Ensured the tail is updated correctly when the last node is deleted.
1 parent a2db304 commit 0924dcb

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

data_structures/linked_list/circular_linked_list.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CircularLinkedList:
1818
def __iter__(self) -> Iterator[Any]:
1919
node = self.head
2020
if node is None:
21-
return # No nodes to iterate over
21+
return
2222
while True:
2323
yield node.data
2424
node = node.next_node
@@ -44,7 +44,7 @@ def insert_nth(self, index: int, data: Any) -> None:
4444
if self.head is None:
4545
# List is empty, so new node is both head and tail, pointing to itself
4646
new_node.next_node = new_node
47-
self.tail = self.head = new_node
47+
self.head = self.tail = new_node
4848
elif index == 0: # Insert at head
4949
new_node.next_node = self.head
5050
self.tail.next_node = new_node
@@ -55,7 +55,7 @@ def insert_nth(self, index: int, data: Any) -> None:
5555
temp = temp.next_node
5656
new_node.next_node = temp.next_node
5757
temp.next_node = new_node
58-
if index == len(self) - 1: # Insert at the tail
58+
if temp == self.tail: # Insert at the tail
5959
self.tail = new_node
6060

6161
def delete_front(self) -> Any:

0 commit comments

Comments
 (0)