{"id":5228,"date":"2021-09-23T10:24:29","date_gmt":"2021-09-23T10:24:29","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=5228"},"modified":"2022-11-09T06:45:05","modified_gmt":"2022-11-09T06:45:05","slug":"find-the-first-node-of-the-loop-in-a-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/","title":{"rendered":"Find the first node of the loop in a Linked List"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011528025-Article_185.png\" alt=\"\" \/><\/p>\n<p>In this article we will learn how to detect the starting node of a loop in the singly linked list. A loop in the singly linked list is a condition that arises when the last node of the linked list does not point to NULL and points to some other node.<\/p>\n<h2>How to find first node of loop in a linked list<\/h2>\n<p>In this problem, we are given a linked list with a loop. We have to find the first node of loop in a linked list.<\/p>\n<p>Let\u2019s try to understand  to find first node of loop in a linked list with the help of examples.<\/p>\n<p>Suppose the given list is:<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_1-19.png\" alt=\"\" \/><\/p>\n<ul>\n<li>According to the problem statement, we need to find first node of loop in a linked list. <\/li>\n<li>From the linked list, we can see that there is a loop in the linked list starting at a node with value 3 and containing 3 nodes 3, 5, and 7. The last node of the loop points back to the first node of the loop.<\/li>\n<li>As the node with value 3 is the first node of the loop, so we will return 3 as output.<\/li>\n<\/ul>\n<p>If the given linked list is:<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_4-7.png\" alt=\"\" \/><\/p>\n<ul>\n<li>In this case, our loop comprises 6, 8, and 10 and the node with value 6 is the first node of the loop. So we will output 6.<\/li>\n<\/ul>\n<p>Now, I think from the above examples, it is clear to find starting point of loop in linked list. <\/p>\n<p>Before moving to the approach section to find first node of loop in a linked list, try to think about how you can approach  to find starting point of loop in linked list. What is the first thing that comes to your mind?<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_4-7.png\" alt=\"\" \/><\/p>\n<p>The very first thing that comes to mind is loop detection. Correct, but we can also use hashing to find first node of loop in a linked list. <\/p>\n<ul>\n<li>The hashing solution will require O(n) space for the creation of the hash table, so we are first going to look at hashing approach, and then jump to the efficient loop detection approach. <\/li>\n<\/ul>\n<p>Let us glance at the approach to get a clear look.<\/p>\n<h2>Approach and Algorithm to find starting point of loop in linked list(Hashing)<\/h2>\n<p>In this approach, we will use hashing to solve the problem. Well, how can hashing help us? <\/p>\n<ul>\n<li>If we notice carefully, the first node of the loop is the first element that will appear twice while traversing the linked list with a loop.<\/li>\n<\/ul>\n<p>1) We will first create a set, which will store the addresses of the nodes.<br \/>\n2) Now, we will traverse the given linked list. For every node, we will check if that node is present in the set or not. <\/p>\n<ul>\n<li>If it is not present, then we will simply insert it into the set. <\/li>\n<li>If the node is present in the set, it means that the current node is the first node of the loop. ThAs the first node of the loop will be the first repeating node while traversing the list. So, if the node is present in the set, we will simply return that node.<\/li>\n<\/ul>\n<p>This approach will work fine for the program to detect the starting node of a loop in the single linked list, but it requires extra O(n) space. So, now the main question is can we optimize this space?<\/p>\n<ul>\n<li>The answer is yes, and we will see how we can optimize this space in the next approach find starting point of loop in linked list .<\/li>\n<\/ul>\n<p>Our next approach uses Floyd\u2019s Cycle Detection algorithm. <\/p>\n<h2>Approach and Algorithm find starting point of loop in linked list(Floyd\u2019s Cycle Detection)<\/h2>\n<p>Our approach will be simple to find first node of loop in a linked list:<\/p>\n<ol>\n<li> Firstly, we have to detect the loop in the given linked list.<\/li>\n<ul>\n<li>For detecting a loop in any linked list, we know the most efficient algorithm is the <strong>Floyd Cycle detection Algorithm<\/strong>. <\/li>\n<\/ul>\n<li>In Floyd&#8217;s cycle detection algorithm, we initialize 2 pointers, <strong>slow<\/strong> and <strong>fast<\/strong>.<\/li>\n<ul>\n<li>Both initially point to the head of the list. <\/li>\n<li>The <strong>slow<\/strong> pointer jumps one place and the <strong>fast<\/strong> pointer jumps 2 places. <\/li>\n<li>The node at which the <strong>slow<\/strong> and <strong>fast<\/strong> pointer meet is a <strong>loop node<\/strong>.<\/li>\n<\/ul>\n<li> Now, after finding the loop node: <\/li>\n<ul>\n<li>We will make 2 pointers <strong>ptr1<\/strong> and <strong>ptr2<\/strong>, which will point to that <strong>loop node<\/strong>. <\/li>\n<li>We will also initialize a variable to find the length of the loop. Now, how will we find the length of this loop?\n<ul>\n<li><strong>ptr2<\/strong> will be fixed, and we will increment <strong>ptr1<\/strong> till it meets <strong>ptr2<\/strong>. As both are at the same position currently, when <strong>ptr1<\/strong> will meet <strong>ptr2<\/strong>, the whole loop would&#8217;ve been traversed, and we will get the length <strong>k<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<li>Now, as we have the length of the loop, we will make <strong>ptr1<\/strong> and <strong>ptr2<\/strong> point to the <strong>head<\/strong> again. <\/li>\n<li>Now, we will shift <strong>ptr2<\/strong> by <strong>k<\/strong> places. <\/li>\n<li> After shifting, we will move both <strong>ptr1<\/strong> and <strong>ptr2<\/strong> simultaneously (taking 1 jump). The node at which <strong>ptr1<\/strong> and <strong>ptr2<\/strong> will meet will be the first node of the loop.<\/li>\n<li>After finding the first node of the loop, we will return it.<\/li>\n<\/ol>\n<h3>Dry Run to find first node of loop in a linked list<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_5-3.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_6-3.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_7-3.png\" alt=\"\" \/><\/p>\n<h2>Approach and Algorithm to find starting point of loop in linked list(More Optimized Approach)<\/h2>\n<p>In the previous method, after finding the loop node, we were also running a for loop to find the length of the loop in the linked list. Can we find the first node of the loop without finding the length of the loop?<\/p>\n<ul>\n<li>The answer is Yes, and in this approach, we will see how we can do it.<\/li>\n<\/ul>\n<p>This method will be very much similar to the previous one, but here we will try to avoid finding the length of the loop.<\/p>\n<ol>\n<li>Similar to the previous method, we will find the loop node using <strong>Floyd Cycle detection Algorithm<\/strong>.<\/li>\n<li>After finding the <strong>loop node<\/strong>, we will initialize the <strong>slow<\/strong> pointer to the <strong>head<\/strong> of the linked list and the <strong>fast<\/strong> pointer will remain at its position.<\/li>\n<li>Now we will start moving the <strong>fast<\/strong> and <strong>slow<\/strong> pointer one node at a time until they meet each other.<\/li>\n<li>The node at which <strong>slow<\/strong> and <strong>fast<\/strong> meets will be the starting point or say the <strong>first node of the loop<\/strong>.<\/li>\n<\/ol>\n<p>Now, let\u2019s see the dry run for this approach.<\/p>\n<h4>Dry Run to find starting point of loop in linked list<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_5-3.png\" alt=\"\" \/><br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_8-4.png\" alt=\"\" \/><\/p>\n<h2>Code Implementation to find first node of loop in a linked list<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_5229 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_5229 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_5229 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_5229 .wpsm_nav-tabs > li.active > a, #tab_container_5229 .wpsm_nav-tabs > li.active > a:hover, #tab_container_5229 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_5229 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_5229 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_5229 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_5229 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_5229 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_5229 .wpsm_nav-tabs > li > a:hover , #tab_container_5229 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_5229 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_5229 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_5229 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_5229 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_5229 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_5229 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_5229 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5229 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_5229 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5229 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_5229 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_5229\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_5229\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_5229_1\" aria-controls=\"tabs_desc_5229_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_5229_2\" aria-controls=\"tabs_desc_5229_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_5229_3\" aria-controls=\"tabs_desc_5229_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_5229_4\" aria-controls=\"tabs_desc_5229_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_5229\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_5229_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n#include&lt;stdbool.h&gt;\r\n\r\nstruct Node {\r\n    int key;\r\n    struct Node* next;\r\n};\r\n\/* Using this we are creating a new list node *\/\r\nstruct Node* newNode(int x)\r\n{\r\n    struct Node* node = malloc(sizeof(struct Node*));\r\n    node-&gt;key = x;\r\n    node-&gt;next = NULL;\r\n    return node;\r\n}\r\n\/* Using this function we will be printing the list *\/\r\nvoid printList(struct Node* head)\r\n{\r\n    while (head != NULL) {\r\n        printf(&quot;%d &quot;,head-&gt;key);\r\n        head = head-&gt;next;\r\n    }\r\n   printf(&quot;&#92;n&quot;);\r\n}\r\n\/* Using this function we will find and return the first node of loop in the linked list *\/\r\nstruct Node* firstnode(struct Node* head)\r\n{\r\n    if (head == NULL || head-&gt;next == NULL)\r\n        return NULL;\r\n    struct Node *slow = head;\r\n    struct Node *fast = head;\r\n    slow = slow-&gt;next;\r\n    fast = fast-&gt;next-&gt;next;\r\n    while (fast &amp;&amp; fast-&gt;next) {\r\n        if (slow == fast)\r\n            break;\r\n        slow = slow-&gt;next;\r\n        fast = fast-&gt;next-&gt;next;\r\n    }\r\n    if (slow != fast)\r\n        return NULL;\r\n    slow = head;\r\n    while (slow != fast) {\r\n        slow = slow-&gt;next;\r\n        fast = fast-&gt;next;\r\n    }\r\n    return slow;\r\n}\r\nint main()\r\n{\r\n    struct Node* head = newNode(1);\r\n    head-&gt;next = newNode(0);\r\n    head-&gt;next-&gt;next = newNode(3);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(0);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(1);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = head-&gt;next;\r\n    struct Node* res = firstnode(head);\r\n    if (res == NULL)\r\n        printf(&quot;Loop does not exist&quot;);\r\n    else\r\n        printf(&quot;Loop starting node is %d&quot;,res-&gt;key);\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_5229_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits stdc++.h=&quot;&quot;&gt;\r\nusing namespace std;\r\n\r\n\/* Structure of a linked list node *\/\r\nstruct Node {\r\n    int key;\r\n    struct Node* next;\r\n};\r\n\r\n\/* Using this we are creating a new list node *\/\r\nNode* newNode(int key)\r\n{\r\n    Node* temp = new Node;\r\n    temp-&gt;key = key;\r\n    temp-&gt;next = NULL;\r\n    return temp;\r\n}\r\n\r\n\/* Using this function we will be printing the list *\/\r\nvoid printList(Node* head)\r\n{\r\n    while (head != NULL) {\r\n        cout &lt;&lt; head-&gt;key &lt;&lt; &quot; &quot;;\r\n        head = head-&gt;next;\r\n    }\r\n    cout &lt;&lt; endl;\r\n}\r\n\r\n\/* Using this function we will find and return the first node of loop in the linked list *\/\r\nNode* firstnode(Node* head)\r\n{\r\n\r\n    if (head == NULL || head-&gt;next == NULL)\r\n        return NULL;\r\n\r\n    Node *slow = head, *fast = head;\r\n\r\n    slow = slow-&gt;next;\r\n    fast = fast-&gt;next-&gt;next;\r\n\r\n    while (fast &amp;&amp; fast-&gt;next) {\r\n        if (slow == fast)\r\n            break;\r\n        slow = slow-&gt;next;\r\n        fast = fast-&gt;next-&gt;next;\r\n    }\r\n\r\n    if (slow != fast)\r\n        return NULL;\r\n\r\n    slow = head;\r\n    while (slow != fast) {\r\n        slow = slow-&gt;next;\r\n        fast = fast-&gt;next;\r\n    }\r\n\r\n    return slow;\r\n}\r\n\r\nint main()\r\n{\r\n    Node* head = newNode(1);\r\n    head-&gt;next = newNode(0);\r\n    head-&gt;next-&gt;next = newNode(3);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(0);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(1);\r\n\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = head-&gt;next;\r\n\r\n    Node* res = firstnode(head);\r\n    if (res == NULL)\r\n        cout &lt;&lt; &quot;Loop does not exist&quot;;\r\n    else\r\n        cout &lt;&lt; &quot;Loop starting node is &quot; &lt;&lt; res-&gt;key;\r\n\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_5229_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\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=\"\">\r\nimport java.util.*;\r\npublic class PrepBytes{\r\n\r\n\/* Structure of a linked list node *\/\r\nstatic class Node\r\n{\r\n    int key;\r\n    Node next;\r\n};\r\n\r\n\/* Using this we are creating a new list node *\/\r\nstatic Node newNode(int key)\r\n{\r\n    Node temp = new Node();\r\n    temp.key = key;\r\n    temp.next = null;\r\n    return temp;\r\n}\r\n\r\n\/* Using this function we will be printing the list *\/\r\nstatic void printList(Node head)\r\n{\r\n    while (head != null){\r\n        System.out.print(head.key + &quot; &quot;);\r\n        head = head.next;\r\n    }\r\n    System.out.println();\r\n}\r\n\r\n\/* Using this function we will find and return the first node of loop in the linked list *\/\r\nstatic Node firstnode(Node head)\r\n{\r\n    if (head == null || head.next == null)\r\n    return null;\r\n    \r\n    Node slow = head, fast = head;\r\n    \r\n    slow = slow.next;\r\n    fast = fast.next.next;\r\n    \r\n    while (fast != null &amp;&amp; fast.next != null){\r\n        if (slow == fast)\r\n        break;\r\n        \r\n        slow = slow.next;\r\n        fast = fast.next.next;\r\n    }\r\n    \r\n    if (slow != fast)\r\n    return null;\r\n\r\n    slow = head;\r\n    while (slow != fast){\r\n        slow = slow.next;\r\n        fast = fast.next;\r\n    }\r\n    \r\n    return slow;\r\n}\r\n\r\npublic static void main(String[] args)\r\n{\r\n    Node head = newNode(1);\r\n    head.next = newNode(0);\r\n    head.next.next = newNode(3);\r\n    head.next.next.next = newNode(0);\r\n    head.next.next.next.next = newNode(1);\r\n    \r\n    head.next.next.next.next.next = head.next;\r\n    \r\n    Node res = firstnode(head);\r\n    if (res == null)\r\n    System.out.print(&quot;Loop does not exist&quot;);\r\n    else\r\n    System.out.print(&quot;Loop starting node is &quot; + res.key);\r\n}\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_5229_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node:\r\n    \r\n    def __init__(self, key):\r\n        \r\n        self.key = key\r\n        self.next = None\r\n\r\ndef newNode(key):\r\n\r\n    temp = Node(key)\r\n    return temp\r\n\r\ndef printList(head):\r\n    \r\n    while (head != None):\r\n        print(head.key, end = ' ')\r\n        head = head.next\r\n    \r\n    print()\r\n\r\ndef firstNode(head):\r\n    \r\n    if (head == None or head.next == None):\r\n        return None\r\n\r\n    slow = head\r\n    fast = head\r\n    slow = slow.next\r\n    fast = fast.next.next\r\n\r\n    while (fast and fast.next):\r\n        if (slow == fast):\r\n            break\r\n        \r\n        slow = slow.next\r\n        fast = fast.next.next\r\n\r\n    if (slow != fast):\r\n        return None\r\n\r\n    slow = head\r\n    \r\n    while (slow != fast):\r\n        slow = slow.next\r\n        fast = fast.next\r\n\r\n    return slow\r\n\r\nif __name__=='__main__':\r\n    \r\n    head = newNode(1)\r\n    head.next = newNode(0)\r\n    head.next.next = newNode(3)\r\n    head.next.next.next = newNode(0)\r\n    head.next.next.next.next = newNode(1)\r\n    head.next.next.next.next.next = head.next\r\n\r\n    res = firstNode(head)\r\n     \r\n    if (res == None):\r\n        print(&quot;Loop does not exist&quot;)\r\n    else:\r\n        print(&quot;Loop starting node is &quot; +\r\n              str(res.key))\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_5229 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_5229 a\"),jQuery(\"#tab-content_5229\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<h4>Output<\/h4>\n<p>Loop starting node is 0<\/p>\n<p><strong>Time Complexity:<\/strong> O(n), as no nested traversal is needed.<\/p>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>So, in this article, we have explained the most efficient approach to finding the first node of a loop in a linked list or detecting the starting node of a loop in a single linked list. Multiple concepts have been used here, so it makes this question an important one for coding interviews.<\/p>\n<h2>FAQs related to Bubble Sort in Linked List<\/h2>\n<ol>\n<li><strong>How do you find the starting point of the loop in the linked list?<\/strong><\/li>\n<p>We will find the starting point of the linked list as follows:<\/p>\n<ul>\n<li>We will first create a set, which will store the addresses of the nodes.<\/li>\n<li>We will traverse the linked list and for every given node, we will check If that node is present or not.<\/li>\n<li>If it is not present then we will insert it into the set and if the node is present it means that the current node is starting node of the loop in the linked list.<\/li>\n<\/ul>\n<li><strong>How do you find the loop in the linked list?<\/strong><\/li>\n<p>A loop can be detected by using a slow and fast pointer algorithm. In this algorithm, a slow pointer moves by one node and a fast pointer moves by two nodes and at any point, when the slow and fast pointer points to the same node, it means the loop is detected at that node and if the fast pointer reaches to end, it means linked list doesn\u2019t contain the loop.<\/ol><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we will learn how to detect the starting node of a loop in the singly linked list. A loop in the singly linked list is a condition that arises when the last node of the linked list does not point to NULL and points to some other node. How to find first [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-5228","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Find the first node of the loop in a Linked List<\/title>\n<meta name=\"description\" content=\"Check the most efficient approaches and algorithms with code to find the first node of a loop in a linked list.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Find the first node of the loop in a Linked List\" \/>\n<meta property=\"og:description\" content=\"Check the most efficient approaches and algorithms with code to find the first node of a loop in a linked list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-23T10:24:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-09T06:45:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011528025-Article_185.png\" \/>\n<meta name=\"author\" content=\"PrepBytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"PrepBytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Find the first node of the loop in a Linked List\",\"datePublished\":\"2021-09-23T10:24:29+00:00\",\"dateModified\":\"2022-11-09T06:45:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/\"},\"wordCount\":1344,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011528025-Article_185.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/\",\"name\":\"Find the first node of the loop in a Linked List\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011528025-Article_185.png\",\"datePublished\":\"2021-09-23T10:24:29+00:00\",\"dateModified\":\"2022-11-09T06:45:05+00:00\",\"description\":\"Check the most efficient approaches and algorithms with code to find the first node of a loop in a linked list.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011528025-Article_185.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011528025-Article_185.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linked list articles\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linked-list\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Find the first node of the loop in a Linked List\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\",\"name\":\"PrepBytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"caption\":\"PrepBytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Find the first node of the loop in a Linked List","description":"Check the most efficient approaches and algorithms with code to find the first node of a loop in a linked list.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Find the first node of the loop in a Linked List","og_description":"Check the most efficient approaches and algorithms with code to find the first node of a loop in a linked list.","og_url":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-09-23T10:24:29+00:00","article_modified_time":"2022-11-09T06:45:05+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011528025-Article_185.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Find the first node of the loop in a Linked List","datePublished":"2021-09-23T10:24:29+00:00","dateModified":"2022-11-09T06:45:05+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/"},"wordCount":1344,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011528025-Article_185.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/","name":"Find the first node of the loop in a Linked List","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011528025-Article_185.png","datePublished":"2021-09-23T10:24:29+00:00","dateModified":"2022-11-09T06:45:05+00:00","description":"Check the most efficient approaches and algorithms with code to find the first node of a loop in a linked list.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011528025-Article_185.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645011528025-Article_185.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/find-the-first-node-of-the-loop-in-a-linked-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linked list articles","item":"https:\/\/prepbytes.com\/blog\/category\/linked-list\/"},{"@type":"ListItem","position":3,"name":"Find the first node of the loop in a Linked List"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e","name":"PrepBytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","caption":"PrepBytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=5228"}],"version-history":[{"count":9,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5228\/revisions"}],"predecessor-version":[{"id":10395,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5228\/revisions\/10395"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=5228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=5228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=5228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}