-
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
Ports-Elise #3
base: master
Are you sure you want to change the base?
Ports-Elise #3
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.
Very fast and nicely done. Take a look at my comments and let me know if you have any questions. Mostly small things I noticed.
# Time Complexity: O(1) | ||
# Space Complexity: O(1) | ||
def add_first(value) | ||
temp_node = @head |
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.
Do you need this temp node?
# Space Complexity: O(1) | ||
def find_max | ||
if @head == nil | ||
return @head |
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 just return nil
?
# Space Complexity: O(1) | ||
def length | ||
length = 0 | ||
if @head == nil |
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.
Do you need this if statement?
# method to delete the first node found with specified value | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def delete(value) |
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.
nicely done
# returns the value at the middle element in the singly linked list | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def find_middle_value |
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.
Nicely done, you didn't even use length
!
|
||
temp_node = @head | ||
idx = 0 | ||
while temp_node != nil |
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 could also use the method to get at an index here.
# returns true if a cycle is found, false otherwise. | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def has_cycle |
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.
👍 Nicely done, classic solution.
# method that inserts a given value as a new last node in the linked list | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def add_last(value) |
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.
Would adding an instance variable @tail
which refers to the last node in a linked list make this method easier or more efficient?
def add_last(value) | ||
raise NotImplementedError | ||
temp_node = @head | ||
while temp_node != nil |
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.
With these types of loops could you instead do until temp_node.next.nil?
# list is sorted in ascending order | ||
# Time Complexity: | ||
# Space Complexity | ||
def insert_ascending(value) |
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.
What about if you inserted a node which was less than the head of a long list.
No description provided.