{"id":590,"date":"2013-05-24T10:37:29","date_gmt":"2013-05-24T09:37:29","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=590"},"modified":"2013-05-24T21:08:10","modified_gmt":"2013-05-24T20:08:10","slug":"floyds-cycle-finding-algorithm","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=590","title":{"rendered":"Floyd&#8217;s Cycle-Finding Algorithm"},"content":{"rendered":"<p>If you want to check if your linked list is circular you can use \u00a0Floyd&#8217;s Cycle-Finding Algorithm aka\u00a0<code>Tortoise and the Hare Algorithm<\/code>.<\/p>\n<p><em>It&#8217;s quite efficient as\u00a0<\/em><em id=\"__mceDel\">It has O(n) complexity.<br \/>\n<!--more--><br \/>\n<\/em><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full\" alt=\"\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/img_519f305ae8487.png\" \/><\/p>\n<pre lang=\"cpp\">struct Node {\n  int data;\n  Node* next;\n}\n\nbool hasLoop(Node* startNode) {\n  \/\/ Init start settings\n  Node* slowNode = startNode;\n  Node* fastNode = startNode->next;\n\n  \/\/ Search until we reached end of linked list of find cycle\n  while (true) {\n    if (fastNode == NULL || fastNode-&gt;next == NULL)\n      return false;\n    \/\/ if fastNode or it's successor reaches slowNode \n    \/\/ we have a circular list\n    else if (slowNode == fastNode || slowNode == fastNode-&gt;next) \n      return true;\n    slowNode = slowNode-&gt;next;\n    fastNode = fastNode-&gt;next-&gt;next;\n  }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want to check if your linked list is circular you can use \u00a0Floyd&#8217;s Cycle-Finding Algorithm aka\u00a0Tortoise and the Hare Algorithm. It&#8217;s quite efficient as\u00a0It has O(n) complexity.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Floyd's Cycle-Finding Algorithm in C++","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15,150,9],"tags":[148,149,157,84],"class_list":["post-590","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-c-development","category-development","tag-algorithms-2","tag-cpp","tag-cycle","tag-list"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-9w","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=590"}],"version-history":[{"count":7,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/590\/revisions"}],"predecessor-version":[{"id":594,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/590\/revisions\/594"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}