Created
October 22, 2015 19:56
-
-
Save gagaception/3c6c080fd4bb0e1b818e to your computer and use it in GitHub Desktop.
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
class LinkedListNode | |
attr_accessor :value, :next_node | |
def initialize(value, next_node=nil) | |
@value = value | |
@next_node = next_node | |
end | |
end | |
def print_values(list_node) | |
print "#{list_node.value} --> " | |
if list_node.next_node.nil? | |
print "nil\n" | |
return | |
else | |
print_values(list_node.next_node) | |
end | |
end | |
def infinity_loop(list) | |
slow = list | |
fast = list | |
until slow.next_node.nil? or fast.next_node.nil? do | |
slow = slow.next_node | |
fast = fast.next_node.next_node | |
return true if (slow == fast) | |
end | |
false | |
end | |
node1 = LinkedListNode.new(37) | |
node2 = LinkedListNode.new(99, node1) | |
node3 = LinkedListNode.new(12, node2) | |
node1.next_node = node3 | |
#print_values(node3) | |
puts infinity_loop(node3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment