-
Notifications
You must be signed in to change notification settings - Fork 47
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
Sockets - Evelynn #25
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well done! You hit the major learning goals with only a few missteps. Take a look at my comments and let me know if you have any questions.
# Time Complexity: O(1) | ||
# Space Complexity: O(1) | ||
def add_first(value) | ||
if self.length != 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do this without calling the length
method since it doesn't matter how long the list is to add an element to the front.
Also since you're doing .length
, this causes your algorithm to be O(n) time complexity.
def find_min | ||
raise NotImplementedError | ||
current = @head | ||
(self.length - 1).times do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since .length
is a method and it traverses the list, it would be good to save that result in a variable rather than calling the method multiple times.
end | ||
|
||
middle = self.length / 2 | ||
|
||
middle_value = self.get_at_index(middle) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Smart!
# Space Complexity | ||
def add_last(value) | ||
raise NotImplementedError | ||
index_of_n = self.length - 1 - n |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the get_at_index method again?
# Time Complexity: O(n) where n is the number of nodes in the list | ||
# Space Complexity: O(1) | ||
def has_cycle | ||
if self.length <= 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a point, if it has a cycle, then the length method will get stuck in an infinite loop.
if a == b | ||
return true | ||
else | ||
a = a.next |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work if the cycle is more than 2 nodes long.
No description provided.