<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Detecting a loop in single linked list</title>
	<link>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/</link>
	<description>C/C++ Programming Blog</description>
	<pubDate>Tue, 14 Oct 2008 03:53:27 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Kamal</title>
		<link>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-27102</link>
		<dc:creator>Kamal</dc:creator>
		<pubDate>Wed, 23 Jul 2008 02:28:08 +0000</pubDate>
		<guid>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-27102</guid>
		<description>Please suggest how does this implementations looks like...

public Node CheckAndFixIfCycle(){
	Node nodeFirstCounter = nodeHeader.next;
	Node nodeSecondCounter = nodeHeader.next;
	Node nodePrevFirstCounter = null;
	Node nodeToFix = null;
			
	while(nodeSecondCounter.next != null){				
		nodePrevFirstCounter = nodeFirstCounter;
				
		nodeFirstCounter = nodeFirstCounter.next;
		nodeSecondCounter = nodeSecondCounter.next.next;
				
		if(nodeSecondCounter == nodeFirstCounter){
			Console.WriteLine("Cyclic List....");
					
			nodeToFix = nodePrevFirstCounter;
						
			//always prev first pointer would be the culprit/node to be fixed and if we nullify that we have fixed the List
			nodeToFix.next = null;
			break;
		}
	}
			
	return nodeToFix;
}</description>
		<content:encoded><![CDATA[<p>Please suggest how does this implementations looks like&#8230;</p>
<p>public Node CheckAndFixIfCycle(){<br />
	Node nodeFirstCounter = nodeHeader.next;<br />
	Node nodeSecondCounter = nodeHeader.next;<br />
	Node nodePrevFirstCounter = null;<br />
	Node nodeToFix = null;</p>
<p>	while(nodeSecondCounter.next != null){<br />
		nodePrevFirstCounter = nodeFirstCounter;</p>
<p>		nodeFirstCounter = nodeFirstCounter.next;<br />
		nodeSecondCounter = nodeSecondCounter.next.next;</p>
<p>		if(nodeSecondCounter == nodeFirstCounter){<br />
			Console.WriteLine(&#8221;Cyclic List&#8230;.&#8221;);</p>
<p>			nodeToFix = nodePrevFirstCounter;</p>
<p>			//always prev first pointer would be the culprit/node to be fixed and if we nullify that we have fixed the List<br />
			nodeToFix.next = null;<br />
			break;<br />
		}<br />
	}</p>
<p>	return nodeToFix;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nemesis</title>
		<link>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-17213</link>
		<dc:creator>nemesis</dc:creator>
		<pubDate>Thu, 13 Mar 2008 04:23:00 +0000</pubDate>
		<guid>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-17213</guid>
		<description>o(n) doesnt look good, specially because its modifying the original list, i'd rather hash the addresses into an N element hash list with open addressing, it uses less space too(no links sweetie). and is easy to keep track of(no linking either sunshine), just a random thought thou.</description>
		<content:encoded><![CDATA[<p>o(n) doesnt look good, specially because its modifying the original list, i&#8217;d rather hash the addresses into an N element hash list with open addressing, it uses less space too(no links sweetie). and is easy to keep track of(no linking either sunshine), just a random thought thou.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ponnada</title>
		<link>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-15938</link>
		<dc:creator>ponnada</dc:creator>
		<pubDate>Tue, 22 Jan 2008 17:32:54 +0000</pubDate>
		<guid>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-15938</guid>
		<description>pratap, there is no issue in the algorithm, it terminates when list has loop because n1 == n2, and that is the start of the loop so it returns from that location. In fact the only difference from your mentioned piece of code and the listed code is listed code moves 2nd pinter at +1, your code moves 2nd pointer at +2 speed.</description>
		<content:encoded><![CDATA[<p>pratap, there is no issue in the algorithm, it terminates when list has loop because n1 == n2, and that is the start of the loop so it returns from that location. In fact the only difference from your mentioned piece of code and the listed code is listed code moves 2nd pinter at +1, your code moves 2nd pointer at +2 speed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pratap Nirujogi</title>
		<link>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-1047</link>
		<dc:creator>Pratap Nirujogi</dc:creator>
		<pubDate>Fri, 25 May 2007 16:43:02 +0000</pubDate>
		<guid>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-1047</guid>
		<description>I think we need to increment the pointer to the node 'n2' one more time in the for loop of the algorithm to find the loop in the list.With the present implemention, for loop in the algorithm will never terminate on trying to detect the loop,when a loop is present(because no pointer will be equal to NULL on incrementing because of the loop).Anyways, to fix the problem I feel we must do the following correction in the for loop:

if(n2 &#038;&#038; (n2-&gt;next)){
  n2 = ((n2-&gt;next)-&gt;next);
}
else{
 // no loop found
 return;
}</description>
		<content:encoded><![CDATA[<p>I think we need to increment the pointer to the node &#8216;n2&#8242; one more time in the for loop of the algorithm to find the loop in the list.With the present implemention, for loop in the algorithm will never terminate on trying to detect the loop,when a loop is present(because no pointer will be equal to NULL on incrementing because of the loop).Anyways, to fix the problem I feel we must do the following correction in the for loop:</p>
<p>if(n2 &#038;&#038; (n2->next)){<br />
  n2 = ((n2->next)->next);<br />
}<br />
else{<br />
 // no loop found<br />
 return;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ponnada</title>
		<link>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-1022</link>
		<dc:creator>ponnada</dc:creator>
		<pubDate>Wed, 02 May 2007 12:29:37 +0000</pubDate>
		<guid>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-1022</guid>
		<description>As the nodes will be malloc'ed, so we are going to get memory from heap, so it is a true location, all nodes will have different addresses (and there is no way they are related)...</description>
		<content:encoded><![CDATA[<p>As the nodes will be malloc&#8217;ed, so we are going to get memory from heap, so it is a true location, all nodes will have different addresses (and there is no way they are related)&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sandeep</title>
		<link>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-1020</link>
		<dc:creator>sandeep</dc:creator>
		<pubDate>Tue, 01 May 2007 05:53:56 +0000</pubDate>
		<guid>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-1020</guid>
		<description>ohh okay, but i have a small question,

the address that a pointer shows, is it the true location or just a relative indicator?</description>
		<content:encoded><![CDATA[<p>ohh okay, but i have a small question,</p>
<p>the address that a pointer shows, is it the true location or just a relative indicator?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ponnada</title>
		<link>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-1015</link>
		<dc:creator>ponnada</dc:creator>
		<pubDate>Thu, 26 Apr 2007 07:39:46 +0000</pubDate>
		<guid>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-1015</guid>
		<description>how can we say the next is address is new address?, again we need to look back at our old traversed nodes to find out whether it is a new address or old address, for doing this it will cost us O(n2).</description>
		<content:encoded><![CDATA[<p>how can we say the next is address is new address?, again we need to look back at our old traversed nodes to find out whether it is a new address or old address, for doing this it will cost us O(n2).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sandeep</title>
		<link>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-1014</link>
		<dc:creator>sandeep</dc:creator>
		<pubDate>Wed, 25 Apr 2007 20:32:35 +0000</pubDate>
		<guid>http://www.openasthra.com/c-tidbits/detecting-a-loop-in-single-linked-list/#comment-1014</guid>
		<description>Can we not just run a loop checking for where the next is pointing?
so long as it finds a new address there is no loop.</description>
		<content:encoded><![CDATA[<p>Can we not just run a loop checking for where the next is pointing?<br />
so long as it finds a new address there is no loop.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
