-
Notifications
You must be signed in to change notification settings - Fork 49
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
Dani - Leaves #33
base: master
Are you sure you want to change the base?
Dani - Leaves #33
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.
Nice work, you hit all the learning goals here. Well done. Take a look at my comments and let me know if you have questions. The methods you have written all work and you've demonstrated an understanding of how to implement a Linked List. There are a few like delete
which aren't done. Let me know if you have questions on how to implement them.
@@ -19,54 +19,125 @@ def initialize | |||
# method to add a new node with the specific data value in the linked list | |||
# insert the new node at the beginning of the linked list | |||
def add_first(value) | |||
raise NotImplementedError | |||
new_node = Node.new(value, @head) | |||
@head = new_node | |||
end | |||
|
|||
# method to find if the linked list contains a node with specified value | |||
# returns true if found, false otherwise | |||
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.
👍
|
||
current = current.next | ||
end | ||
return found | ||
end | ||
|
||
# method to return the max value in the linked list | ||
# returns the data value and not the node | ||
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.
👍
lib/linked_list.rb
Outdated
return nil if @head == nil | ||
|
||
current = @head | ||
min = 0 |
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 all the elements are negative numbers?
min = 0 | |
min = current.data |
end | ||
current = current.next | ||
end | ||
return min | ||
end | ||
|
||
|
||
# method that returns the length of the singly linked list | ||
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 = current.next | ||
end | ||
return length | ||
end | ||
|
||
# method that returns the value at a given index in the linked list | ||
# index count starts at 0 | ||
# returns nil if there are fewer nodes in the linked list than the index value | ||
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.
👍
end | ||
|
||
# method to delete the first node found with specified value | ||
def delete(value) | ||
raise NotImplementedError | ||
return nil if @head == nil | ||
end | ||
|
||
# method to reverse the singly linked list | ||
# note: the nodes should be moved and not just the values in the nodes | ||
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.
👍
@@ -79,7 +150,25 @@ def find_middle_value | |||
# find the nth node from the end and return its value | |||
# assume indexing starts at 0 while counting to n | |||
def find_nth_from_end(n) |
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.
👍
@@ -94,18 +183,41 @@ def has_cycle | |||
# returns the value in the first node | |||
# returns nil if the list is empty | |||
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.
👍
return @head | ||
else | ||
return @head.data | ||
end | ||
end | ||
|
||
# method that inserts a given value as a new last node in the linked list | ||
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.
👍 Good use of add_first
current = current.next | ||
end | ||
current.next = new_last_node | ||
end | ||
end | ||
|
||
# method that returns the value of the last node in the linked list | ||
# returns nil if the linked list is empty | ||
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.
👍
No description provided.