<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>linked list program in clinked list algorithm - Wikitechy</title>
	<atom:link href="https://www.wikitechy.com/interview-questions/tag/linked-list-program-in-clinked-list-algorithm/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.wikitechy.com/interview-questions/tag/linked-list-program-in-clinked-list-algorithm/</link>
	<description>Interview Questions</description>
	<lastBuildDate>Thu, 16 Sep 2021 12:32:43 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.wikitechy.com/interview-questions/wp-content/uploads/2025/10/cropped-wikitechy-icon-32x32.png</url>
	<title>linked list program in clinked list algorithm - Wikitechy</title>
	<link>https://www.wikitechy.com/interview-questions/tag/linked-list-program-in-clinked-list-algorithm/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Detect loop in a linked list ?</title>
		<link>https://www.wikitechy.com/interview-questions/data-structure/detect-loop-in-a-linked-list/</link>
					<comments>https://www.wikitechy.com/interview-questions/data-structure/detect-loop-in-a-linked-list/#respond</comments>
		
		<dc:creator><![CDATA[Editor]]></dc:creator>
		<pubDate>Tue, 13 Jul 2021 11:08:43 +0000</pubDate>
				<category><![CDATA[Data Structure]]></category>
		<category><![CDATA[Accenture interview questions and answers]]></category>
		<category><![CDATA[Altimetrik India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Applied Materials interview questions and answers]]></category>
		<category><![CDATA[Bharti Airtel interview questions and answers]]></category>
		<category><![CDATA[BMC Software interview questions and answers]]></category>
		<category><![CDATA[Capgemini interview questions and answers]]></category>
		<category><![CDATA[CASTING NETWORKS INDIA PVT LIMITED interview questions and answers]]></category>
		<category><![CDATA[CGI Group Inc interview questions and answers]]></category>
		<category><![CDATA[Chetu interview questions and answers]]></category>
		<category><![CDATA[Ciena Corporation interview questions and answers]]></category>
		<category><![CDATA[Collabera Te interview questions and answers]]></category>
		<category><![CDATA[Dell International Services India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[find length of linked list java]]></category>
		<category><![CDATA[find length of loop in linked list]]></category>
		<category><![CDATA[find length of loop in linked list in java]]></category>
		<category><![CDATA[find start of loop in linked list]]></category>
		<category><![CDATA[find start of loop in linked list java]]></category>
		<category><![CDATA[Flipkart interview questions and answers]]></category>
		<category><![CDATA[floyd's cycle detection algorithm]]></category>
		<category><![CDATA[for loop linked list c++]]></category>
		<category><![CDATA[Genpact interview questions and answers]]></category>
		<category><![CDATA[Globallogic India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[IBM interview questions and answers]]></category>
		<category><![CDATA[Indecomm Global Services interview questions and answers]]></category>
		<category><![CDATA[java detect]]></category>
		<category><![CDATA[length of loop in linked list]]></category>
		<category><![CDATA[length of loop in linked list java]]></category>
		<category><![CDATA[linked list algorithm]]></category>
		<category><![CDATA[linked list in data structure]]></category>
		<category><![CDATA[linked list program in c with explanation]]></category>
		<category><![CDATA[linked list program in clinked list algorithm]]></category>
		<category><![CDATA[Mphasis interview questions and answers]]></category>
		<category><![CDATA[n'th node from end of linked list]]></category>
		<category><![CDATA[NetApp interview questions and answers]]></category>
		<category><![CDATA[Oracle Corporation interview questions and answers]]></category>
		<category><![CDATA[reverse a linked list]]></category>
		<category><![CDATA[samsung interview questions and answers]]></category>
		<category><![CDATA[SAP Labs India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Sapient Consulting Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Tech Mahindra interview questions and answers]]></category>
		<category><![CDATA[telibrahma interview questions and answers]]></category>
		<category><![CDATA[Tracxn Technologies Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[UnitedHealth Group interview questions and answers]]></category>
		<category><![CDATA[what is loop]]></category>
		<category><![CDATA[Wipro Infotech interview questions and answers]]></category>
		<category><![CDATA[WM Global Technology Services India Pvt.Ltd Limited (WMGTS) interview questions and answers]]></category>
		<category><![CDATA[Xoriant Solutions Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Yodlee Infotech Pvt Ltd interview questions and answers]]></category>
		<guid isPermaLink="false">https://www.wikitechy.com/interview-questions/?p=473</guid>

					<description><![CDATA[Answer : There are two ways to detect loop in linked list...
]]></description>
										<content:encoded><![CDATA[<div class="TextHeading">
<div class="hddn">
<h2 id="detect-loop-in-a-linked-list" class="color-pink">Detect loop in a linked list ?</h2>
</div>
</div>
<div class="Content">
<div class="hddn">
<ul>
<li>There are two ways to detect loop in linked list or not.</li>
</ul>
</div>
</div>
<div class="ImageContent">
<div class="hddn"><img decoding="async" class="img-responsive center-block aligncenter" src="https://cdn.wikitechy.com/interview-questions/data-structure/detect-loop-in-a-linked-list.png" alt=" Detect Loop in a Linked List" /></div>
</div>
<div class="TextHeading">
<div class="hddn">
<h2 id="method-1" class="color-blue">Method 1:</h2>
</div>
</div>
<div class="Content">
<div class="hddn">
<ul>
<li>Traverse through each node till end , tracking visited node using Hash map.</li>
<li>If you find node that is already visited, then there is a loop in LinkedList</li>
<li>If you reach till end while traversing then there is no loop in LinkedList</li>
</ul>
<div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-markdown code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-markdown code-embed-code">using namespace std; <br/><br/>struct Node <br/>{ <br/>	int data; <br/>	struct Node* next; <br/>}; <br/><br/>void push(struct Node** head_ref, int new_data) <br/>{ <br/><br/>	struct Node* new_node = new Node; <br/><br/>	<br/>	new_node-&gt;data = new_data; <br/><br/>	/* link the old list off the new node */<br/>	new_node-&gt;next = (*head_ref); <br/><br/>	/* move the head to point to the new node */<br/>	(*head_ref) = new_node; <br/>} <br/><br/>// Returns true if there is a loop in linked list <br/>// else returns false. <br/>bool detectLoop(struct Node *h) <br/>{ <br/>	unordered_set&lt;Node *&gt; s; <br/>	while (h != NULL) <br/>	{ <br/>		// If this node is already present <br/>		// in hashmap it means there is a cycle <br/>		// (Because you we encountering the <br/>		// node for the second time). <br/>		if (s.find(h) != s.end()) <br/>			return true; <br/><br/>		// If we are seeing the node for <br/>		// the first time, insert it in hash <br/>		s.insert(h); <br/><br/>		h = h-&gt;next; <br/>	} <br/><br/>	return false; <br/>} <br/><br/>/* Drier program to test above function*/<br/>int main() <br/>{ <br/>	/* Start with the empty list */<br/>	struct Node* head = NULL; <br/><br/>	push(&amp;head, 21); <br/>	push(&amp;head, 14); <br/>	push(&amp;head, 5); <br/>	push(&amp;head, 10); <br/><br/>	/* Create a loop for testing */<br/>	head-&gt;next-&gt;next-&gt;next-&gt;next = head; <br/><br/>	if (detectLoop(head)) <br/>		cout &lt;&lt; &quot;Loop found&quot;; <br/>	else<br/>		cout &lt;&lt; &quot;No Loop&quot;; <br/><br/>	return 0; <br/>} </code></pre> </div>
<div class="TextHeading">
<div class="hddn">
<h2 id="output" class="color-blue">Output:</h2>
<p><code class="hljs" data-lang=""><span class="nt">Loop found</span></code></p>
</div>
<div class="TextHeading">
<div class="hddn">
<h2 id="method-2" class="color-blue">Method 2:</h2>
</div>
</div>
<div class="Content">
<div class="hddn">
<ul>
<li>Efficient approach for this problem would be Floyd’s cycle detection algorithm, so steps for this algorithm would be:</li>
</ul>
</div>
</div>
<div class="Content">
<div class="hddn">
<ul>
<li style="list-style-type: none;">
<ul>
<li>Use two pointer fastPtr and slowPtr and initialize both to head of linkedlist</li>
<li>Move fastPtr by two nodes and slowPtr by one node in each iteration.</li>
<li>If fastPtr and slowPtr meet at some iteration , then there is a loop in linkedlist.</li>
<li>If fastPtr reaches to the end of linkedlist without meeting slow pointer then there is no loop in linkedlist (i.e fastPtr-&gt;next or fastPtr-&gt;next-&gt;next become null).</li>
</ul>
</li>
</ul>
<div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-c code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-c code-embed-code">// C program to detect loop in a linked list <br/>#include&lt;stdio.h&gt; <br/>#include&lt;stdlib.h&gt; <br/>/* Link list node */<br/>struct Node <br/>{ <br/>	int data; <br/>	struct Node* next; <br/>}; <br/><br/>void push(struct Node** head_ref, int new_data) <br/>{ <br/>	/* allocate node */<br/>	struct Node* new_node = <br/>		(struct Node*) malloc(sizeof(struct Node)); <br/><br/>	/* put in the data */<br/>	new_node-&gt;data = new_data; <br/><br/>	/* link the old list off the new node */<br/>	new_node-&gt;next = (*head_ref); <br/><br/>	/* move the head to point to the new node */<br/>	(*head_ref) = new_node; <br/>} <br/><br/>int detectloop(struct Node *list) <br/>{ <br/>	struct Node *slow_p = list, *fast_p = list; <br/><br/>	while (slow_p &amp;&amp; fast_p &amp;&amp; fast_p-&gt;next ) <br/>	{ <br/>		slow_p = slow_p-&gt;next; <br/>		fast_p = fast_p-&gt;next-&gt;next; <br/>		if (slow_p == fast_p) <br/>		{ <br/>		printf(&quot;Found Loop&quot;); <br/>		return 1; <br/>		} <br/>	} <br/>	return 0; <br/>} <br/><br/>/* Drier program to test above function*/<br/>int main() <br/>{ <br/>	/* Start with the empty list */<br/>	struct Node* head = NULL; <br/><br/>	push(&amp;head, 10); <br/>	push(&amp;head, 4); <br/>	push(&amp;head, 5); <br/>	push(&amp;head, 10); <br/><br/>	/* Create a loop for testing */<br/>	head-&gt;next-&gt;next-&gt;next-&gt;next = head; <br/>	detectloop(head); <br/><br/>	return 0; <br/>}</code></pre> </div>
<div class="TextHeading">
<div class="hddn">
<h2 id="output-2" class="color-blue">Output:</h2>
</div>
</div>
<div class="CodeContent">
<div class="hddn">
<figure class="highlight">
<pre><code id="code2" class="hljs" data-lang=""><span class="nt">Found Loop</span></code></pre>
</figure>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.wikitechy.com/interview-questions/data-structure/detect-loop-in-a-linked-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
