Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
Not bad, you made a few mistakes with the Big-O and one method delete doesn't work, but otherwise you did quite well. Check out my comments and let me know if you have any questions.
| # method to delete the first node found with specified value | ||
| # Time Complexity: O(n) where n is the length of the linke list | ||
| # Space Complexity: O(1) | ||
| def delete(value) |
There was a problem hiding this comment.
This one isn't working at all.
Here's a couple of suggestions:
- You're using
@headin theuntilloop. You shouldn't do that in the as current will be moving through the list, well past@head. - You are doing
last.next.nextwithout checking to ensure thatlast.nextis notnil.
You should also have a previous variable to keep track of the node before current.
| # Space Complexity | ||
| def has_cycle | ||
| raise NotImplementedError | ||
| while fast != nil && fast.next != nil |
There was a problem hiding this comment.
I like how you're doing it without having to find the entire length first!
| # assume indexing starts at 0 while counting to n | ||
| # Time Complexity: O(n) where n is the length of the linked list | ||
| # Space Complexity: O(1) | ||
| def find_nth_from_end(n) |
There was a problem hiding this comment.
Could you do this one in a similar manner to has_cycle so that you don't have to find the length before finding the nth from the end?
| # linked list links to a node already visited. | ||
| # returns true if a cycle is found, false otherwise. | ||
| # Time Complexity: O(n) where n is the length of the linked list | ||
| # Space Complexity: O(1) |
There was a problem hiding this comment.
Space complexity of O(1)? Really? You're making an array visited which will get as large as the list.
You are also using visited.include?(current) which will do a loop through the array to find current. So you have a loop in a loop or an O(n2) algorithm.
No description provided.