Open
Conversation
CheezItMan
reviewed
Feb 23, 2020
CheezItMan
left a comment
There was a problem hiding this comment.
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) | |||
|
|
||
| # method to find if the linked list contains a node with specified value | ||
| # returns true if found, false otherwise | ||
| def search(value) |
|
|
||
| # method to return the max value in the linked list | ||
| # returns the data value and not the node | ||
| def find_max |
lib/linked_list.rb
Outdated
| end | ||
|
|
||
| curr = @head | ||
| self.(length-1).times do |
There was a problem hiding this comment.
This has a typo
Suggested change
| self.(length-1).times do | |
| (self.length - 1).times do | |
|
|
||
| # method to return the min value in the linked list | ||
| # returns the data value and not the node | ||
| def find_min |
|
|
||
| # 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.
This doesn't seem to work as you never change @head
|
|
||
| ## Advanced Exercises | ||
| # returns the value at the middle element in the singly linked list | ||
| 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) |
| # 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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
You'll want to leave the loop when you add the new node.
Suggested change
| curr.next = new | |
| curr.next = new | |
| return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.