Created
September 29, 2015 00:58
-
-
Save kenmazaika/82fe5ede651777934346 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 floyd?(list_node) | |
| tortoise = list_node.next_node | |
| hare = list_node.next_node.next_node | |
| while tortoise.value != hare.value | |
| begin | |
| tortoise = tortoise.next_node | |
| hare = hare.next_node.next_node | |
| rescue | |
| return false | |
| end | |
| return true | |
| end | |
| end | |
| node1 = LinkedListNode.new(37) | |
| node2 = LinkedListNode.new(99, node1) | |
| node3 = LinkedListNode.new(12, node2) | |
| #node1.next_node = node3 | |
| puts floyd?(node2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment