06. Find Length of Loop
β GFG solution to Find Length of Loop problem: detect and count nodes in a linked list loop using Floyd's Cycle Detection Algorithm (Tortoise and Hare). π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given the head of a linked list, determine whether the list contains a loop. If a loop is present, return the number of nodes in the loop, otherwise return 0.
Note: Internally, pos(1 based index) is used to denote the position of the node that tail's next pointer is connected to. If pos = 0, it means the last node points to null, indicating there is no loop. Note that pos is not passed as a parameter.
π Examples
Example 1
Input: pos = 2
Output: 4
Explanation: There exists a loop in the linked list and the length of the loop is 4.
Example 2
Input: pos = 3
Output: 3
Explanation: The loop is from 19 to 10. So length of loop is 19 β 33 β 10 = 3.
Example 3
Input: pos = 0
Output: 0
Explanation: There is no loop.
π Constraints
$1 \le \text{number of nodes} \le 10^5$
$1 \le \text{node->data} \le 10^4$
$0 \le \text{pos} < \text{number of nodes}$
β
My Approach
The optimal approach uses Floyd's Cycle Detection Algorithm (also known as the Tortoise and Hare Algorithm):
Floyd's Cycle Detection + Loop Length Calculation
Phase 1 - Loop Detection:
Use two pointers:
slow
(moves 1 step) andfast
(moves 2 steps).If there's a loop,
slow
andfast
will eventually meet inside the loop.If
fast
reaches NULL, there's no loop.
Phase 2 - Loop Length Calculation:
Once loop is detected (when
slow == fast
), keep one pointer fixed.Move the other pointer one step at a time until it completes one full cycle.
Count the steps taken - this gives the loop length.
Edge Cases:
If no loop exists, return 0.
Handle single node and empty list cases.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the number of nodes in the linked list. In the worst case, we traverse the entire list once to detect the loop, and then traverse the loop once more to count its length.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for the two pointers, regardless of the size of the linked list.
π§βπ» Code (C++)
class Solution {
public:
int lengthOfLoop(Node* head) {
Node *s = head, *f = head;
while (f && f->next) {
s = s->next;
f = f->next->next;
if (s == f) {
int cnt = 1;
while (s->next != f) s = s->next, cnt++;
return cnt;
}
}
return 0;
}
};
β Code (Java)
class Solution {
public int lengthOfLoop(Node head) {
Node s = head, f = head;
while (f != null && f.next != null) {
s = s.next;
f = f.next.next;
if (s == f) {
int cnt = 1;
while (s.next != f) {
s = s.next;
cnt++;
}
return cnt;
}
}
return 0;
}
}
π Code (Python)
class Solution:
def lengthOfLoop(self, head):
s, f = head, head
while f and f.next:
s, f = s.next, f.next.next
if s == f:
cnt = 1
while s.next != f:
s, cnt = s.next, cnt + 1
return cnt
return 0
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated