The Wayback Machine - https://web.archive.org/web/20201021161422/https://github.com/donnemartin/interactive-coding-challenges/issues/224
Skip to content
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

Linked_List_Challenge get length method- another solution? #224

Open
redlonr opened this issue Jan 8, 2018 · 2 comments
Open

Linked_List_Challenge get length method- another solution? #224

redlonr opened this issue Jan 8, 2018 · 2 comments
Labels

Comments

@redlonr
Copy link

@redlonr redlonr commented Jan 8, 2018

I'm not sure this is necessarily an issue: The current solution of linked_list_challenge.ipynb uses an iterative approach to getting the length of a linked list, resulting in an O(n) time complexity for that method. Another solution could be to have an instance variable, updating this variable whenever we insert_to_front(), append(), or delete() and return this variable whenever we call the length method (thus O(1)) . I'm not a python expert, so I don't know how "Pythonic" this solution is, but I got it working in my notebook and can submit a PR if this is appropriate

@rodferro
Copy link

@rodferro rodferro commented Mar 3, 2018

Yeah, I thought about that too. Though I'm also not a Python expert, I've seen a similar solution in the book "Data Structures and Algorithms in Python" by Goodrich et al.

@redlonr
Copy link
Author

@redlonr redlonr commented Mar 5, 2018

I made PR #227 to address this. Also added a test for an edge case- deletion of the head node of a linked list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.