{"id":3405,"date":"2021-07-26T09:17:55","date_gmt":"2021-07-26T09:17:55","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=3405"},"modified":"2023-11-27T05:21:26","modified_gmt":"2023-11-27T05:21:26","slug":"find-the-length-of-a-loop-in-the-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/","title":{"rendered":"Find the length of a loop in the linked list"},"content":{"rendered":"<p>In the realm of data structures, linked lists are foundational, offering dynamic memory allocation and efficient data manipulation. However, linked lists can occasionally contain loops or cycles, which disrupt their linear structure. Determining the length of a loop within a linked list poses an intriguing challenge for developers and requires a nuanced understanding of algorithms and pointer manipulation. This article aims to explore methodologies and algorithms to find the length of a loop within a linked list, elucidating step-by-step approaches to solve this common problem in data structure manipulation.<\/p>\n<h2>How to find the length of loop in linked list<\/h2>\n<p>In this problem, we are given a LinkedList (root node) and are asked to find the length of the cycle\/loop that is present in the LinkedList. Let me explain this with an example:-<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/07\/38_1-1.png\" alt=\"\" \/> <\/p>\n<p>The above-Linked List has a loop that has 6 nodes in the loop\/cycle. Hence, this shall return 6 as the answer.<br \/>\nWell, the very first task that I can observe is to determine whether the LinkedList contains a cycle or not. So, it shall follow the same algorithm, and then we can try to think of any modifications to find its length also.<\/p>\n<h2>Approach #1 to find the length of loop in linked list<\/h2>\n<p>The first approach is based on maps. The idea is to store the address of the nodes as the key and their position as the values. So, when we traverse and insert into the map if we come across a node that points to an address that is already present in the map that means there is a cycle present in the LinkedList. From this, we can find the length by simply subtracting the current position from the position of the matched node as <code>position - map[currnode]<\/code>.<\/p>\n<h2>Algorithm to find the length of loop in linked list<\/h2>\n<ul>\n<li>Start traversing every node of the linked list present and maintain the position(incremented with every node) in the map.<\/li>\n<li>While inserting also check if that node is present in the map, that will mean we have come across that node and there is a cycle cause we are visiting the node again.\n<ul>\n<li>If the node is not present in the map &#8211; increment the position counter and insert the current node address in the map.<\/li>\n<li>If the node is present in the map &#8211; then there is a cycle and we can find the number of nodes from <code>position - map[currnode]<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Code Implementation to find the length of loop in 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_3408 {\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_3408 .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_3408 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_3408 .wpsm_nav-tabs > li.active > a, #tab_container_3408 .wpsm_nav-tabs > li.active > a:hover, #tab_container_3408 .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_3408 .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_3408 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_3408 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_3408 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_3408 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_3408 .wpsm_nav-tabs > li > a:hover , #tab_container_3408 .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_3408 .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_3408 .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_3408 .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_3408 .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_3408 .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_3408 .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_3408 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3408 .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_3408 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3408 .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_3408 .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_3408\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_3408\">\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_3408_1\" aria-controls=\"tabs_desc_3408_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_3408_2\" aria-controls=\"tabs_desc_3408_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>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_3408\">\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_3408_1\">\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 <bits stdc++.h>\r\nusing namespace std;\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n\r\n    Node(int num)\r\n    {\r\n        data = num;\r\n        next = NULL;\r\n    }\r\n};\r\nint countNodesinLoop(struct Node* head)\r\n{\r\n    struct Node* p = head;\r\n    int pos = 0;\r\n    unordered_map<node*, int=\"\"> m;\r\n    while (p != NULL) {\r\n        if (m.find(p) == m.end()) {\r\n            m[p] = pos;\r\n            pos++;\r\n        }\r\n        else {\r\n            return (pos - m[p]);\r\n        }\r\n        p = p->next;\r\n    }\r\n    return 0;\r\n}\r\nint main()\r\n{\r\n    struct Node* head = new Node(1);\r\n    head->next = new Node(2);\r\n    head->next->next = new Node(3);\r\n    head->next->next->next = new Node(4);\r\n    head->next->next->next->next = new Node(5); \r\n    head->next->next->next->next->next = new Node(6); \r\n    head->next->next->next->next->next->next = head->next;\r\n    cout << countNodesinLoop(head) << endl; \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_3408_2\">\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\tdef __init__(self, val):\r\n\t\tself.val = val\r\n\t\tself.next = None\r\n\t\r\nclass LinkedList:\r\n\t\r\n\tdef __init__(self):\r\n\t\tself.head = None\t\r\n\r\n\tdef AddNode(self, val):\r\n\t\tif self.head is None:\r\n\t\t\tself.head = Node(val)\r\n\t\telse:\r\n\t\t\tcurr = self.head\r\n\t\t\twhile(curr.next):\r\n\t\t\t\tcurr = curr.next\r\n\t\t\tcurr.next = Node(val)\r\n\r\n\tdef countNodesinLoop(self):\r\n\t\t\r\n\t\tp = self.head\r\n\t\tpos = 0\r\n\t\tm = dict()\r\n\r\n\t\twhile p:\r\n\t\t\t\r\n\t\t\tif p not in m:\r\n\t\t\t\tm[p] = pos\r\n\t\t\t\tpos += 1\r\n\r\n\t\t\telse:\r\n\t\t\t\treturn pos - m[p]\r\n\r\n\t\t\tp = p.next\r\n\r\n\t\treturn 0\r\n\t\r\nmyLL = LinkedList()\r\nmyLL.AddNode(1)\r\nmyLL.AddNode(2)\r\nmyLL.AddNode(3)\r\nmyLL.AddNode(4)\r\nmyLL.AddNode(5)\r\nmyLL.AddNode(6)\r\n\r\nmyLL.head.next.next.next.next.next.next = myLL.head.next\r\nloopLength = myLL.countNodesinLoop()\r\nif myLL.head is None:\r\n\tprint(\"Linked list is empty\")\r\nelse:\r\n\tprint(str(loopLength))\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\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_3408 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_3408 a\"),jQuery(\"#tab-content_3408\"));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<pre><code>Output: 5<\/code><\/pre>\n<p><strong>Time Complexity of the length of loop in linked list<\/strong>: O(n), where n is the number of nodes.<br \/>\n<strong>Space complexity of the length of loop in linked list<\/strong>: O(n), for using a map <\/p>\n<p>As you can see, this method uses some extra space, we have to try to think of something better to reduce the extra space at least. <\/p>\n<h2>Approach #2 to find the length of loop in linked list<\/h2>\n<p>The next idea is based on Floyd\u2019s Cycle detection algorithm. The concept is to use two pointers, one fast-moving another slow-moving. Both the pointers traverse the linked list with different speeds and when they meet each other that means there\u2019s a cycle present in the LinkedList. Save the address of this node and take a counter with 1 and start incrementing it while traversing the LinkedList again from the common point with another pointer. Once we reach the common pointer again, then we will have our number of nodes in cycle count in the pointer. We will return this count.<\/p>\n<h2>Algorithm to find length of loop in linked list<\/h2>\n<ul>\n<li>Take two pointers, a fast pointer, and a slow pointer pointing to the head initially.<\/li>\n<li>Traverse both the pointers as <code>slowptr = slowptr-&gt;next<\/code> (1 node at a time), and <code>fastptr = fastptr-&gt;next-&gt;next<\/code> (2 nodes at a time).<\/li>\n<li>When slowptr == fastptr, the common point is the node for the head of the cycle.<\/li>\n<li>Fix one pointer to this node and take count = 0 and move the other pointer from the common point one by one in the linked list and increment the counter by 1 in each step<\/li>\n<li>When the other pointer reaches the common point then stop the iteration and return the count.<\/li>\n<\/ul>\n<h2>Code Implementation to find length of loop in 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_3410 {\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_3410 .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_3410 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_3410 .wpsm_nav-tabs > li.active > a, #tab_container_3410 .wpsm_nav-tabs > li.active > a:hover, #tab_container_3410 .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_3410 .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_3410 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_3410 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_3410 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_3410 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_3410 .wpsm_nav-tabs > li > a:hover , #tab_container_3410 .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_3410 .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_3410 .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_3410 .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_3410 .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_3410 .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_3410 .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_3410 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3410 .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_3410 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3410 .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_3410 .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_3410\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_3410\">\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_3410_1\" aria-controls=\"tabs_desc_3410_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_3410_2\" aria-controls=\"tabs_desc_3410_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_3410_3\" aria-controls=\"tabs_desc_3410_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_3410_4\" aria-controls=\"tabs_desc_3410_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_3410\">\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_3410_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\r\n#include&lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n\r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node* next;\r\n};\r\nint countNodes(struct Node *n)\r\n{\r\n    int res = 1;\r\n    struct Node *temp = n;\r\n    while (temp-&gt;next != n)\r\n    {\r\n        res++;\r\n        temp = temp-&gt;next;\r\n    }\r\n    return res;\r\n}\r\nint countNodesinLoop(struct Node *list)\r\n{\r\n    struct Node *slow_p = list, *fast_p = list;\r\n    while (slow_p &amp;&amp; fast_p &amp;&amp; fast_p-&gt;next)\r\n    {\r\n        slow_p = slow_p-&gt;next;\r\n        fast_p = fast_p-&gt;next-&gt;next;\r\n    \r\n        if (slow_p == fast_p)\r\n            return countNodes(slow_p);\r\n    }\r\n    return 0;\r\n}\r\nstruct Node* newNode(int x)\r\n{\r\n    struct Node* node = malloc(sizeof(struct Node*));\r\n    node-&gt;data = x;\r\n    node-&gt;next = NULL;\r\n    return node;\r\n}\r\nint main()\r\n{\r\n    struct Node *head = newNode(1);\r\n    head-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next = newNode(3);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(4);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(5);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(6); \r\n     head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = head-&gt;next;\r\n     int ans = countNodesinLoop(head);\r\n     printf(&quot;%d &#92;n&quot;,ans);\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_3410_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<bits stdc++.h>\r\nusing namespace std;\r\n\r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node* next;\r\n};\r\nint countNodes(struct Node *n)\r\n{\r\n    int res = 1;\r\n    struct Node *temp = n;\r\n    while (temp->next != n)\r\n    {\r\n        res++;\r\n        temp = temp->next;\r\n    }\r\n    return res;\r\n}\r\nint countNodesinLoop(struct Node *list)\r\n{\r\n    struct Node *slow_p = list, *fast_p = list;\r\n\r\n    while (slow_p && fast_p && fast_p->next)\r\n    {\r\n        slow_p = slow_p->next;\r\n        fast_p = fast_p->next->next;\r\n    \r\n        if (slow_p == fast_p)\r\n            return countNodes(slow_p);\r\n    }\r\n    return 0;\r\n}\r\n\r\nstruct Node *newNode(int key)\r\n{\r\n    struct Node *temp = new Node();\r\n    temp->data = key;\r\n    temp->next = NULL;\r\n    return temp;\r\n}\r\nint main()\r\n{\r\n    struct Node *head = newNode(1);\r\n    head->next = newNode(2);\r\n    head->next->next = newNode(3);\r\n    head->next->next->next = newNode(4);\r\n    head->next->next->next->next = newNode(5);\r\n    head->next->next->next->next->next = new Node(6); \r\n     head->next->next->next->next->next->next = head->next;\r\n    cout << countNodesinLoop(head) << endl;\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_3410_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\nclass LoopI \r\n{\r\n\t\r\n    static class Node\r\n    {\r\n\t    int data;\r\n\t    Node next;\r\n\t    Node(int data)\r\n\t    {\r\n\t\t    this.data =data;\r\n\t\t    next =null;\r\n\t    }\r\n    }\r\n    static int countNodes( Node n)\r\n    {\r\n        int res = 1;\r\n        Node temp = n;\r\n        while (temp.next != n)\r\n        {\r\n\t        res++;\r\n\t        temp = temp.next;\r\n        }\r\n        return res;\r\n    }\r\n    static int countNodesinLoop( Node list)\r\n    {\r\n\t    Node slow_p = list, fast_p = list;\r\n        while (slow_p !=null && fast_p!=null && fast_p.next!=null)\r\n\t    {\r\n\t\t    slow_p = slow_p.next;\r\n\t\t    fast_p = fast_p.next.next;\r\n\r\n\t\t    if (slow_p == fast_p)\r\n\t\t\t    return countNodes(slow_p);\r\n\t    }\r\n        return 0;\r\n    }\r\n    static Node newNode(int key)\r\n    {\r\n\t    Node temp = new Node(key);\r\n\t\r\n\t    return temp;\r\n    }\r\n\tpublic static void main (String[] args) \r\n    {\r\n\t\tNode head = newNode(1);\r\n\t    head.next = newNode(2);\r\n\t    head.next.next = newNode(3);\r\n\t    head.next.next.next = newNode(4);\r\n\t    head.next.next.next.next = newNode(5);\r\n\r\n\t    \/* Create a loop for testing *\/\r\n\t    head.next.next.next.next.next = head.next;\r\n\r\n\t    System.out.println( countNodesinLoop(head));\r\n\t}\r\n}\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_3410_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\tdef __init__(self, val):\r\n\t\tself.val = val\r\n\t\tself.next = None\r\n\t\r\nclass LinkedList:\r\n\t\r\n\tdef __init__(self):\r\n\t\tself.head = None\t\r\n\r\n\tdef AddNode(self, val):\r\n\t\tif self.head is None:\r\n\t\t\tself.head = Node(val)\r\n\t\telse:\r\n\t\t\tcurr = self.head\r\n\t\t\twhile(curr.next):\r\n\t\t\t\tcurr = curr.next\r\n\t\t\tcurr.next = Node(val)\r\n\r\n\tdef countNodesinLoop(self):\r\n\t\t\r\n\t\tif self.head is None:\r\n\t\t\treturn 0\r\n\t\t\r\n\t\tslow = self.head\r\n\t\tfast = self.head\r\n\t\tflag = 0 \r\n\r\n\t\twhile(slow and slow.next and fast and\r\n\t\t\tfast.next and fast.next.next):\r\n\t\t\tif slow == fast and flag != 0:\r\n\t\t\t\tcount = 1\r\n\t\t\t\tslow = slow.next\r\n\t\t\t\twhile(slow != fast):\r\n\t\t\t\t\tslow = slow.next\r\n\t\t\t\t\tcount += 1\r\n\t\t\t\treturn count\r\n\t\t\t\r\n\t\t\tslow = slow.next\r\n\t\t\tfast = fast.next.next\r\n\t\t\tflag = 1\r\n\t\treturn 0 \r\n\t\r\nmyLL = LinkedList()\r\nmyLL.AddNode(1)\r\nmyLL.AddNode(2)\r\nmyLL.AddNode(3)\r\nmyLL.AddNode(4)\r\nmyLL.AddNode(5)\r\nmyLL.AddNode(6)\r\n\r\nmyLL.head.next.next.next.next.next.next = myLL.head.next\r\nloopLength = myLL.countNodesinLoop()\r\nif myLL.head is None:\r\n\tprint(\"Linked list is empty\")\r\nelse:\r\n\tprint(str(loopLength))\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\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_3410 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_3410 a\"),jQuery(\"#tab-content_3410\"));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<pre><code>Output: 5<\/code><\/pre>\n<p><strong>Space complexity to find length of cycle in linked list<\/strong>: O(1), no extra space is used.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nIdentifying and determining the length of a loop within a linked list demands a deep understanding of pointer manipulation and algorithmic techniques. Various methods, such as Floyd&#8217;s Cycle Detection Algorithm or using additional data structures, enable programmers to detect loops and calculate their lengths effectively. This knowledge proves invaluable in maintaining the integrity of linked list operations, enhancing algorithm efficiency, and ensuring robustness in data structure manipulation. By mastering these techniques, programmers can adeptly navigate and manage linked lists containing loops, enabling the development of more resilient and optimized software systems.<\/p>\n<h2>FAQs related to the length of loop in linked list<\/h2>\n<p>Here are some FAQs related to the Length of Loop in Linked List.<\/p>\n<p><strong>1. What is a loop in a linked list?<\/strong><br \/>\nA loop in a linked list refers to a scenario where a node within the list, rather than pointing to the next node in sequence, points to a node that is earlier in the list, creating a cycle or loop within the structure.<\/p>\n<p><strong>2. How do you detect the presence of a loop in a linked list?<\/strong><br \/>\nSeveral algorithms, such as Floyd&#8217;s Cycle Detection Algorithm (also known as the &quot;Tortoise and Hare&quot; algorithm), can be employed to detect the presence of a loop in a linked list. By using two pointers moving at different speeds through the list, this algorithm can identify if and where the pointers meet, indicating the existence of a loop.<\/p>\n<p><strong>3. How can you find the length of a loop in a linked list?<\/strong><br \/>\nOnce a loop is detected within a linked list, determining its length involves intricate pointer manipulation. One approach involves using the meeting point of the pointers (found using Floyd&#8217;s algorithm) as a reference to traverse the loop, counting the number of nodes until the pointers meet again to calculate the loop&#8217;s length.<\/p>\n<p><strong>4. Are there other methods to find the length of a loop?<\/strong><br \/>\nAlternative methods to find the length of a loop in a linked list include utilizing a hash table to store visited nodes or modifying the data structure by introducing additional pointers or attributes to mark and count nodes while detecting loops.<\/p>\n<p><strong>5. Why is determining the length of a loop important in linked list manipulation?<\/strong><br \/>\nUnderstanding the length of a loop in a linked list is crucial for various applications, including memory management, algorithmic optimizations, and ensuring the integrity of linked list operations. It aids in designing algorithms that handle cyclic structures efficiently and accurately.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the realm of data structures, linked lists are foundational, offering dynamic memory allocation and efficient data manipulation. However, linked lists can occasionally contain loops or cycles, which disrupt their linear structure. Determining the length of a loop within a linked list poses an intriguing challenge for developers and requires a nuanced understanding of algorithms [&hellip;]<\/p>\n","protected":false},"author":52,"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-3405","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 length of a loop in the linked list | PrepBytes Blog<\/title>\n<meta name=\"description\" content=\"Learn the most efficient way to find the length of a cycle 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-length-of-a-loop-in-the-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 length of a loop in the linked list | PrepBytes Blog\" \/>\n<meta property=\"og:description\" content=\"Learn the most efficient way to find the length of a cycle in a linked list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-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-07-26T09:17:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-27T05:21:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/07\/38_1-1.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=\"5 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-length-of-a-loop-in-the-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Find the length of a loop in the linked list\",\"datePublished\":\"2021-07-26T09:17:55+00:00\",\"dateModified\":\"2023-11-27T05:21:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/\"},\"wordCount\":1138,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/07\/38_1-1.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/\",\"name\":\"Find the length of a loop in the linked list | PrepBytes Blog\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/07\/38_1-1.png\",\"datePublished\":\"2021-07-26T09:17:55+00:00\",\"dateModified\":\"2023-11-27T05:21:26+00:00\",\"description\":\"Learn the most efficient way to find the length of a cycle in a linked list.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/07\/38_1-1.png\",\"contentUrl\":\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/07\/38_1-1.png\",\"width\":2063,\"height\":539},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-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 length of a loop in the 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\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Find the length of a loop in the linked list | PrepBytes Blog","description":"Learn the most efficient way to find the length of a cycle 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-length-of-a-loop-in-the-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Find the length of a loop in the linked list | PrepBytes Blog","og_description":"Learn the most efficient way to find the length of a cycle in a linked list.","og_url":"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-07-26T09:17:55+00:00","article_modified_time":"2023-11-27T05:21:26+00:00","og_image":[{"url":"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/07\/38_1-1.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Find the length of a loop in the linked list","datePublished":"2021-07-26T09:17:55+00:00","dateModified":"2023-11-27T05:21:26+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/"},"wordCount":1138,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/07\/38_1-1.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/","name":"Find the length of a loop in the linked list | PrepBytes Blog","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/07\/38_1-1.png","datePublished":"2021-07-26T09:17:55+00:00","dateModified":"2023-11-27T05:21:26+00:00","description":"Learn the most efficient way to find the length of a cycle in a linked list.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-linked-list\/#primaryimage","url":"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/07\/38_1-1.png","contentUrl":"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/07\/38_1-1.png","width":2063,"height":539},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/find-the-length-of-a-loop-in-the-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 length of a loop in the 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\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3405","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\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=3405"}],"version-history":[{"count":9,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3405\/revisions"}],"predecessor-version":[{"id":18385,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3405\/revisions\/18385"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=3405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=3405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=3405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}