Conversation
spitsfire
left a comment
There was a problem hiding this comment.
Looks like you are missing the last two problems!
| # Space Complexity: ? | ||
| # Time Complexity: O(1) accessing it only on | ||
| # Space Complexity: O(1) no new data structure being created | ||
| def get_first(self): |
| # Space Complexity: ? | ||
| # Time Complexity: o(1) | ||
| # Space Complexity: o(1) constant time, since new data structure is being created but its small | ||
| def add_first(self, value): |
| # returns true if found, false otherwise | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def search(self, value): |
| def search(self, value): | ||
| pass | ||
| current = self.head | ||
| while current is not None: |
There was a problem hiding this comment.
we can shorten it to:
| while current is not None: | |
| while current: |
| # Time Complexity: ? | ||
| # Space Complexity: ? |
There was a problem hiding this comment.
What do you think the space and time complexity of this is? Is the time complexity dependent on the length of the linked list? Is there any new variables being created that grow larger depending upon the input?
| # method that returns the length of the singly linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: 0(1) constant time |
There was a problem hiding this comment.
This should be O(n). The while loop is dependent upon the length of the linked list (ie, the input) to find the appropriate node
| length += 1 | ||
| current = current.next | ||
|
|
||
| return length |
There was a problem hiding this comment.
Move this line out of the while loop; otherwise, it will return immediately after the first iteration:
| return length | |
| return length |
| # Space Complexity: ? | ||
| def get_at_index(self, index): | ||
| pass | ||
| if self.length() < index: |
There was a problem hiding this comment.
love this! use those helper methods!
| # returns None if there are fewer nodes in the linked list than the index value | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def get_at_index(self, index): |
There was a problem hiding this comment.
great start, but if the index is less than the length of the linked list, how do we find the appropriate node value at that index?
| # returns None if the linked list is empty | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def get_last(self): |
There was a problem hiding this comment.
How do we travel to the down to the last node and return its value?
No description provided.