-
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
Space - Hala #37
base: master
Are you sure you want to change the base?
Space - Hala #37
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 well done. The code works and you can take a look at my comments for suggestions. Let me know if you have questions here.
# 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.
👍
# 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(1) | ||
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.
👍
# Time Complexity: O(N) | ||
# Space Complexity: O(1) | ||
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.
👍
# 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.
👍
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be simplified a bit:
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 | |
# Time Complexity: O(N) | ||
# Space Complexity: O(1) | ||
def get_at_index(index) |
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 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.
👍
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit overcomplicated
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 | |
# 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.
👍
No description provided.