{"id":283,"date":"2009-06-15T21:39:57","date_gmt":"2009-06-16T04:39:57","guid":{"rendered":"http:\/\/new-gro.dyn.hhhh.org\/joeboy\/blog\/?p=283"},"modified":"2010-01-25T14:02:37","modified_gmt":"2010-01-25T21:02:37","slug":"algorithm-to-find-the-center-of-a-list-in-on-time","status":"publish","type":"post","link":"https:\/\/www.hhhh.org\/joeboy\/blog\/?p=283","title":{"rendered":"Algorithm to find the center of a list in O(n) time"},"content":{"rendered":"<p>Same technique can be used to find loops in a liked list. If you advance through the list one pointer at rate X, and another pointer through the list at rate (1\/2)X then when the faster pointer is at the end of the list the slower pointer will be pointing to the center element of the list.<\/p>\n<p>&lt;pre class=code&gt;<\/p>\n<p>\/\/ findCenterElement &#8211; finds the element at the center of the list<\/p>\n<p>node *findCenter(node *curList){<\/p>\n<p>node *centerNode = curList;<\/p>\n<p>node *curNode = curList;<\/p>\n<p>if(curList == (node *)NULL)<\/p>\n<p>return((node *)NULL);<\/p>\n<p>if(curNode-&gt;next != (node *)NULL){<\/p>\n<p>while(((curNode-&gt;next)-&gt;next) != (node *)NULL)<\/p>\n<p>{<\/p>\n<p>curNode = (curNode-&gt;next)-&gt;next;<\/p>\n<p>centerNode = (centerNode-&gt;next);<\/p>\n<p>}<\/p>\n<p>}else{<\/p>\n<p>return(curNode);<\/p>\n<p>}<\/p>\n<p>return(centerNode);<\/p>\n<p>}<\/p>\n<p>&lt;\/pre&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Same technique can be used to find loops in a liked list. If you advance through the list one pointer at rate X, and another pointer through the list at rate (1\/2)X then when the faster pointer is at the end of the list the slower pointer will be pointing to the center element of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[],"class_list":["post-283","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.hhhh.org\/joeboy\/blog\/index.php?rest_route=\/wp\/v2\/posts\/283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hhhh.org\/joeboy\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hhhh.org\/joeboy\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hhhh.org\/joeboy\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hhhh.org\/joeboy\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=283"}],"version-history":[{"count":3,"href":"https:\/\/www.hhhh.org\/joeboy\/blog\/index.php?rest_route=\/wp\/v2\/posts\/283\/revisions"}],"predecessor-version":[{"id":518,"href":"https:\/\/www.hhhh.org\/joeboy\/blog\/index.php?rest_route=\/wp\/v2\/posts\/283\/revisions\/518"}],"wp:attachment":[{"href":"https:\/\/www.hhhh.org\/joeboy\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hhhh.org\/joeboy\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hhhh.org\/joeboy\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}