-
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
KS #40
base: master
Are you sure you want to change the base?
KS #40
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -21,39 +21,85 @@ def initialize | |||||
# Time Complexity: | ||||||
# Space Complexity | ||||||
def add_first(value) | ||||||
raise NotImplementedError | ||||||
new_node = Node.new(value) | ||||||
|
||||||
if @head | ||||||
new_node.next = @head | ||||||
end | ||||||
|
||||||
@head = new_node | ||||||
end | ||||||
|
||||||
# method to find if the linked list contains a node with specified value | ||||||
# returns true if found, false otherwise | ||||||
# Time Complexity: | ||||||
# Space Complexity | ||||||
def search(value) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||
raise NotImplementedError | ||||||
current = @head | ||||||
while current | ||||||
if current.data == value | ||||||
return true | ||||||
end | ||||||
current = current.next | ||||||
end | ||||||
return false | ||||||
end | ||||||
|
||||||
# method to return the max value in the linked list | ||||||
# returns the data value and not the node | ||||||
# Time Complexity: | ||||||
# Space Complexity | ||||||
def find_max | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||
raise NotImplementedError | ||||||
if !@head | ||||||
return nil | ||||||
end | ||||||
|
||||||
current = @head | ||||||
max = current.data | ||||||
current = current.next | ||||||
while current | ||||||
if current.data > max | ||||||
max = current.data | ||||||
end | ||||||
current = current.next | ||||||
end | ||||||
return max | ||||||
end | ||||||
|
||||||
# method to return the min value in the linked list | ||||||
# returns the data value and not the node | ||||||
# Time Complexity: | ||||||
# Space Complexity | ||||||
def find_min | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||
raise NotImplementedError | ||||||
if !@head | ||||||
return nil | ||||||
end | ||||||
|
||||||
current = @head | ||||||
min = current.data | ||||||
current = current.next | ||||||
while current | ||||||
if current.data < min | ||||||
min = current.data | ||||||
end | ||||||
current = current.next | ||||||
end | ||||||
return min | ||||||
end | ||||||
|
||||||
|
||||||
# method that returns the length of the singly linked list | ||||||
# Time Complexity: | ||||||
# Space Complexity | ||||||
def length | ||||||
raise NotImplementedError | ||||||
i = 0 | ||||||
current = @head | ||||||
|
||||||
while current | ||||||
i++ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++ doesn't really work as an operator in Ruby. It treats this as i plus some positive number.
Suggested change
|
||||||
current = current.next | ||||||
end | ||||||
return i | ||||||
end | ||||||
|
||||||
# method that returns the value at a given index in the linked list | ||||||
|
@@ -69,14 +115,32 @@ def get_at_index(index) | |||||
# Time Complexity: | ||||||
# Space Complexity | ||||||
def visit | ||||||
raise NotImplementedError | ||||||
current = @head | ||||||
while current | ||||||
print "#{current.data} " | ||||||
current = current.next | ||||||
end | ||||||
end | ||||||
|
||||||
# method to delete the first node found with specified value | ||||||
# Time Complexity: | ||||||
# Space Complexity | ||||||
def delete(value) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||
raise NotImplementedError | ||||||
if !@head | ||||||
return | ||||||
end | ||||||
if @head.data == value | ||||||
@head = @head.next | ||||||
return | ||||||
end | ||||||
current = @head | ||||||
while current.next | ||||||
if current.next.data == value | ||||||
current.next = current.next.next | ||||||
return | ||||||
end | ||||||
current = current.next | ||||||
end | ||||||
end | ||||||
|
||||||
# method to reverse the singly linked list | ||||||
|
@@ -120,7 +184,8 @@ def has_cycle | |||||
# Time Complexity: | ||||||
# Space Complexity | ||||||
def get_first | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||||||
raise NotImplementedError | ||||||
return nil if !@head | ||||||
return @head.data | ||||||
end | ||||||
|
||||||
# method that inserts a given value as a new last node in the linked list | ||||||
|
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 & Space Complexity?