-
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 - Yoyo #38
base: master
Are you sure you want to change the base?
Time - Yoyo #38
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.
Some issues here and some existing syntax errors. Take a look at my comments and let me know what questions you have.
# Time Complexity: O(1) | ||
# Space Complexity: O(1) | ||
def add_first(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.
👍
new_node = Node.new(value) | ||
if @head.nil? | ||
@head = new_node | ||
else | ||
new_node.next = @head | ||
@head = new_node | ||
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.
This can be simplified as:
new_node = Node.new(value) | |
if @head.nil? | |
@head = new_node | |
else | |
new_node.next = @head | |
@head = new_node | |
end | |
@head = Node.new(value, @head) |
# Time Complexity: O(1) | ||
# 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.
👍 The time complexity here is O(n)
new_previous = current.next | ||
new_current = new_previous.next | ||
new_previous.next = current | ||
current = new_current |
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.
Missingend
current = new_current | |
current = new_current | |
end |
if @head.nil? | ||
return nil | ||
end | ||
previous = @head | ||
current = @head | ||
until current.data == value || current.nil? | ||
previous = current | ||
current = current.next | ||
end | ||
previous.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.
You also need a check here to see if value
is at the head of the list.
if @head.nil? | |
return nil | |
end | |
previous = @head | |
current = @head | |
until current.data == value || current.nil? | |
previous = current | |
current = current.next | |
end | |
previous.next == current.next | |
if @head.nil? | |
return nil | |
elsif @head.data == value | |
@head = @head.next | |
return | |
end | |
previous = @head | |
current = @head | |
until current.data == value || current.nil? | |
previous = current | |
current = current.next | |
end | |
previous.next == current.next |
# Time Complexity: O(n) | ||
# Space Complexity: O(n) | ||
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.
👍 , but the space complexity is O(1) because you're not making a new list here.
# 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.
end
# Time Complexity: O(1) | ||
# Space Complexity: O(1) | ||
def get_first |
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(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.
👍
# Time Complexity: O(1) | ||
# Space Complexity: O(1) | ||
def get_last |
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.
👍 , but the time complexity is O(n) since you're traveling to the end of the list.
No description provided.