-
-
Notifications
You must be signed in to change notification settings - Fork 46.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Singly_Linked_List: extra O(n) time complexity while getting length of the list #5315
Comments
@cclauss I can fix this, please have a look at it. |
In theory, that statement only adds O(n) time, which if you insert length+=1 / -=1 while adding or removing elements will incur the same extra time i.e the Overall complexity doesn't change. But you're right the creation of a tuple is definitely unnecessary in terms of space. I've made a pull request for the same, check it out: #5320 |
…added/removed. Fixes Issue: TheAlgorithms#5315
As discussed in #5320, maintaining an instance variable may be a minor optimization but it has also proven to be a source of bugs. It is pythonic to call |
While getting the length of the linked list, code will take O(n) of unnecessary time as it executes -
len(tuple(iter(self)))
This also takes up O(n) of extra space as it is creating a tuple first.
This can be easily avoided by maintaining an instance variable
len
, increment/decrement it while inserting/deleting elements.The text was updated successfully, but these errors were encountered: