-
Notifications
You must be signed in to change notification settings - Fork 48
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
Time - Alex #26
base: master
Are you sure you want to change the base?
Time - Alex #26
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.
Overall pretty good, some small bugs that I tried to point out here. Let me know if you have questions about them.
if @head.nil? | ||
@head = Node.new(value) | ||
else | ||
new_nodelet = Node.new(value, @head) | ||
new_nodelet.next = @head | ||
@head = new_nodelet | ||
end |
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 rewrite this as:
if @head.nil? | |
@head = Node.new(value) | |
else | |
new_nodelet = Node.new(value, @head) | |
new_nodelet.next = @head | |
@head = new_nodelet | |
end | |
@head = Node.new(value, @head) |
This way the new node's next points to the old head (which might be nil
).
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def search(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.
👍
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def find_max |
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.
Correct, but space complexity would be O(1) since you don't create a new data structure, but just track the greatest value found so far.
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
def find_min |
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.
Ditto with find_max
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def length |
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.
👍
return nil if @head.nil? | ||
|
||
current = @head | ||
count = 0 | ||
|
||
until count == index | ||
current = current.next | ||
count += 1 | ||
end | ||
|
||
return current.data |
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 if the length is less than index
?
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def visit |
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.
👍
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
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.
What if you are deleting the 1st node?
|
||
last = @head | ||
current = @head | ||
|
||
until current.data == value || current.nil? | ||
last = current | ||
current = current.next | ||
end | ||
|
||
last.next = current.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.
last = @head | |
current = @head | |
until current.data == value || current.nil? | |
last = current | |
current = current.next | |
end | |
last.next = current.next | |
if @head.data == value | |
@head = @head.next | |
return | |
end | |
last = nil | |
current = @head | |
until current.data == value || current.nil? | |
last = current | |
current = current.next | |
end | |
unless current.nil? | |
last.next = current.next | |
end |
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def reverse |
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
No description provided.