Open
Conversation
spitsfire
reviewed
Jul 25, 2022
spitsfire
left a comment
There was a problem hiding this comment.
your space-time complexities look great! great job!
| # Space Complexity: ? | ||
| # Time Complexity: O(1) | ||
| # Space Complexity: O(1) | ||
| def get_first(self): |
| # Space Complexity: ? | ||
| # Time Complexity: O(1) | ||
| # Space Complexity: O(1) | ||
| def add_first(self, value): |
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| def search(self, value): |
| def search(self, value): | ||
| pass | ||
| cur = self.head | ||
| while cur != None: |
There was a problem hiding this comment.
one way we can shorten this is by:
Suggested change
| while cur != None: | |
| while cur: |
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| def length(self): |
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| def get_at_index(self, index): |
Comment on lines
+91
to
+95
| prev = None | ||
| while cur != None: | ||
| prev = cur | ||
| cur = cur.next | ||
| prev.next = Node(value, None) |
There was a problem hiding this comment.
same as above! we can start our while loop at while curnext ! None:
Suggested change
| prev = None | |
| while cur != None: | |
| prev = cur | |
| cur = cur.next | |
| prev.next = Node(value, None) | |
| while cur.next != None: | |
| cur = cur.next | |
| cur.next = Node(value, None) |
|
|
||
| # method to return the max value in the linked list | ||
| # returns the data value and not the node | ||
| def find_max(self): |
Comment on lines
+122
to
+129
| prev = self.head | ||
| cur = self.head.next | ||
| while cur != None and cur.value != value: | ||
| prev = cur | ||
| cur = cur.next | ||
|
|
||
| if cur != None: | ||
| prev.next = cur.next |
There was a problem hiding this comment.
this works! let's take away one of the variables, though. again, let's start the while loop with cur.next != None:
Suggested change
| prev = self.head | |
| cur = self.head.next | |
| while cur != None and cur.value != value: | |
| prev = cur | |
| cur = cur.next | |
| if cur != None: | |
| prev.next = cur.next | |
| cur = self.head | |
| while cur.next != None and cur.next.value != value: | |
| cur = cur.next | |
| cur.next = cur.next.next |
Comment on lines
+151
to
+156
| cur = self.head | ||
| last_made = None | ||
| while cur != None: | ||
| last_made = Node(cur.value, last_made) | ||
| cur = cur.next | ||
| self.head = last_made |
There was a problem hiding this comment.
hmm we shouldn't be adding nodes, only reversing the pointers so the last node is now the head and vice versa.
It's commonly done with three pointers: previous, current, and future node
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.