You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.