Open
Conversation
CheezItMan
reviewed
Sep 1, 2020
CheezItMan
left a comment
There was a problem hiding this comment.
Overall well done. The code works and you can take a look at my comments for suggestions. Let me know if you have questions here.
Comment on lines
+24
to
26
| # Time Complexity: O(1) | ||
| # Space Complexity: O(1) | ||
| def add_first(value) |
Comment on lines
+40
to
42
| # Time Complexity: O(N) | ||
| # Space Complexity: O(1) | ||
| def search(value) |
Comment on lines
+65
to
67
| # Time Complexity: O(N) | ||
| # Space Complexity: O(1) | ||
| def find_max |
Comment on lines
+92
to
94
| # Time Complexity: O(N) | ||
| # Space Complexity: O(1) | ||
| def find_min |
Comment on lines
+119
to
121
| # Time Complexity: O(N) | ||
| # Space Complexity: O(1) | ||
| def length |
Comment on lines
+122
to
+134
| length = 1 | ||
| current = @head | ||
| if current.nil? | ||
| return 0 | ||
| elsif current.next.nil? | ||
| return 1 | ||
| else | ||
| while current.next != nil | ||
| length +=1 | ||
| current = current.next | ||
| end | ||
| end | ||
| return length |
There was a problem hiding this comment.
This could be simplified a bit:
Suggested change
| length = 1 | |
| current = @head | |
| if current.nil? | |
| return 0 | |
| elsif current.next.nil? | |
| return 1 | |
| else | |
| while current.next != nil | |
| length +=1 | |
| current = current.next | |
| end | |
| end | |
| return length | |
| len = 0 | |
| current = @head | |
| until current.nil? | |
| len += 1 | |
| current = current.next | |
| end | |
| return len | |
Comment on lines
+140
to
142
| # Time Complexity: O(N) | ||
| # Space Complexity: O(1) | ||
| def get_at_index(index) |
Comment on lines
+166
to
168
| # Time Complexity: O(N) | ||
| # Space Complexity: O(1) | ||
| def visit |
Comment on lines
+184
to
+199
| current = @head | ||
| while current != nil | ||
| if @head.next.nil? && @head.data == value | ||
| @head = nil | ||
| elsif @head.next != nil && @head.data == value | ||
| @head = @head.next | ||
| elsif current.next != nil && current.prev != nil && current.data == value | ||
| prevn = current.prev | ||
| nextn = current.next | ||
| prevn.next = nextn | ||
| nextn.prev = prevn | ||
| elsif !current.prev.nil? && current.data == value | ||
| current.prev.next = current.next | ||
| end | ||
| current = current.next | ||
| end |
There was a problem hiding this comment.
This is a bit overcomplicated
Suggested change
| current = @head | |
| while current != nil | |
| if @head.next.nil? && @head.data == value | |
| @head = nil | |
| elsif @head.next != nil && @head.data == value | |
| @head = @head.next | |
| elsif current.next != nil && current.prev != nil && current.data == value | |
| prevn = current.prev | |
| nextn = current.next | |
| prevn.next = nextn | |
| nextn.prev = prevn | |
| elsif !current.prev.nil? && current.data == value | |
| current.prev.next = current.next | |
| end | |
| current = current.next | |
| end | |
| if @head.data == value | |
| @head = @head.next | |
| return | |
| end | |
| current = @head | |
| previous = nil | |
| until current.nil? | |
| if current.data == value | |
| previous.next = current.next | |
| return | |
| end | |
| previous = current | |
| current = current.next | |
| end | |
Comment on lines
+204
to
206
| # Time Complexity: O(N) | ||
| # Space Complexity: O(1) | ||
| def reverse |
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.