Skip to content
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

Leaves - Yitgop #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Leaves - Yitgop #41

wants to merge 2 commits into from

Conversation

rinostar
Copy link

No description provided.

@rinostar rinostar changed the title Yitgop -- Leaves Leaves - Yitgop Feb 20, 2020
Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of this is quite well done, you do have several methods that aren't working however. Take a look at my comments and let me know if you have questions. You do seem to understand the essentials of working with linked list nodes however.

@@ -19,99 +19,228 @@ 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -19,99 +19,228 @@ 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
@head = Node.new(value, @head)
end

# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
def search(value)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return true if checker.data == value
checker = checker.next
end
return false
end

# method to return the max value in the linked list
# returns the data value and not the node
def find_max

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

end

curr = @head
self.(length-1).times do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has a typo

Suggested change
self.(length-1).times do
(self.length - 1).times do

max = marker.data if marker.data > max
marker = marker.next
end
return max
end

# method to return the min value in the linked list
# returns the data value and not the node
def find_min

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@head = curr.next
else
previous.next = curr.next
end
end

# method to reverse the singly linked list
# note: the nodes should be moved and not just the values in the nodes
def reverse

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to work as you never change @head

curr.next = prev
prev = curr
curr = temp
end
end


## Advanced Exercises
# returns the value at the middle element in the singly linked list
def find_middle_value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

fast = fast.next.next
end

return slow.data
end

# 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

fast = fast.next
end

return slow.data
end

# checks if the linked list has a cycle. A cycle exists if any node in the
# linked list links to a node already visited.
# returns true if a cycle is found, false otherwise.
def has_cycle

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't quite work as the cycle doesn't have to connect the head to the tail, it could be a circle starting toward the end. Like if the last node points to the 2nd to last node.

until curr.nil?
if value >= curr.data
new = Node.new(value,curr.next.next)
curr.next = new

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll want to leave the loop when you add the new node.

Suggested change
curr.next = new
curr.next = new
return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants