Skip to content

Conversation

PranavYewale
Copy link

Added a length attribute to the CircularLinkedList class to track the length of the linked list.

Updated the len method to return the length attribute instead of calculating the length using a loop. This change improves the space complexity of the len method to O(1).

Updated the insert_tail and insert_head methods to update the length attribute directly instead of calling the len function. This change ensures that the length is accurately maintained.

Updated the insert_nth method to include a check for the index range and raise an IndexError if the index is out of range. This provides better error handling.

Added a delete_tail method that deletes the last node of the linked list. This method calls the delete_nth method with the index set to self.length - 1 and the update_length flag set to True. This ensures that the length is correctly updated when the last node is deleted.

Updated the delete_nth method to include an update_length parameter. When this parameter is set to True, the method will update the length attribute after deleting the node.

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label May 16, 2023
@amirsoroush
Copy link
Contributor

amirsoroush commented May 19, 2023

I'm agree with you that keeping track of the length would give us O(1) time complexity in those operations. Currently in insert_tail method we calculate the length twice: Once in insert_tail and once when it get delegated to insert_nth.

But this idea got rejected in the past. You may want to look at the comments here and here.

Copy link
Contributor

@rohan472000 rohan472000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look in ruff issue, to fix that use ruff . --fix before pushing.

Copy link
Contributor

@tianyizheng02 tianyizheng02 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignoring the arguments for and against having a length attribute, this PR breaks things in other significant ways: removing all imports, removing a required class, removing the test function, and removing the __main__ block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants