{"id":126,"date":"2019-01-21T20:38:12","date_gmt":"2019-01-22T01:38:12","guid":{"rendered":"https:\/\/resrvoir.com\/?page_id=126"},"modified":"2019-07-15T18:32:46","modified_gmt":"2019-07-15T22:32:46","slug":"circular-linked-list","status":"publish","type":"page","link":"https:\/\/resrvoir.com\/?page_id=126","title":{"rendered":"Circular Linked List"},"content":{"rendered":"\n<p>Determine if a linked list is circular.<br><br>The idea here is to move a slow pointer and a fast pointer and if there is a circle, the pointers eventually meet at the same node.<br><br>At each step, slow pointer s moves one node and fast pointer f moves two nodes. If s enters a circle, f will be j nodes ahead of s. Since it&#8217;s a circle, it also means that f is k &#8211; j nodes behind s, where k is number of nodes in the circle. Because s moves forward one node at a time and f moves forward two nodes at a time, f gets one node closer to s at each step.<br><br>Since it takes at most k steps for f to meet with s at the same node, and k &lt;= n where n is number of nodes, the runtime is O(n).<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">bool isCircular(Node head) {\n  Node s = head\n  Node f = head\n\n  while (true) {\n    if (s.next)\n      s = s.next\n    else\n      return false\n\n    if (f.next &amp;&amp; f.next.next)\n      f = f.next.next\n    else\n      return false\n\n    if (f == s)\n      return true\n  }\n  return false\n}<\/pre>\n\n\n\n<p>Here&#8217;s a bit of a math explanation for the circle.<br>k = number of nodes in circle<br>i = number of steps taken after s has entered circle<br>j = number of nodes f is ahead of s after s has entered circle<br>fp = position of fast pointer in circle<br>sp = position of slow pointer in circle<\/p>\n\n\n\n<p>Here is the position sp and fp, at any step i<br>sp = i % k<br>fp = (2 * i + j) % k<br><br>And where sp = fp is where the two pointers meet, or<br>i % k = (2 * i + j) % k<br><br>Let&#8217;s do an example. For a circle of length k = 100 and a fast pointer j = 12 nodes ahead, find i where fp = sp.<br><br>When i is 0, the slow pointer is at 0 and the fast pointer is 12 nodes ahead just as we would expect.<\/p>\n\n\n\n<div class=\"wp-block-columns has-4-columns is-layout-flex wp-container-5 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>0 % 100<br>(2 * 0 + 12) % 100<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>0 = sp<br>12 = fp<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<p>And when i = 88 steps taken, the two pointers meet. This can be simplified to k &#8211; j = 88.<\/p>\n\n\n\n<div class=\"wp-block-columns has-4-columns is-layout-flex wp-container-10 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>88 % 100<br>(2 * 88 + 12) % 100<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>88 = sp<br>88 = fp<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Determine if a linked list is circular. The idea here is to move a slow pointer and a fast pointer and if there is a circle, the pointers eventually meet at the same node. At each step, slow pointer s moves one node and fast pointer f moves two nodes. If s enters a circle, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2,"menu_order":19,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/126"}],"collection":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=126"}],"version-history":[{"count":63,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/126\/revisions"}],"predecessor-version":[{"id":692,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/126\/revisions\/692"}],"up":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}