Find length of loop in a Linked List using Map
Write a program that checks whether a given Linked List contains a loop and if a loop is present then returns the count of nodes in the loop. For example, a loop is present in the below-linked list and the length of the loop is 4. If the loop is not present, then the function should return 0.
Approach: In this post, we will use the concept of Map to store the addresses of nodes present in the linked list as a key and their position as the values.
Below is the step-by-step approach:
- Traverse every node of the linked list and maintain the position starting with one. Increment the position after every node.
- Check whether that node is present in the Map or not.
- If the map does not contain the address of that node, insert it into the map along with its position.
- If the map already contains the address of that node, return the difference between their positions.
- If no such node has been found, return 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
Node( int num)
{
data = num;
next = NULL;
}
};
int countNodesinLoop( struct Node* head)
{
struct Node* p = head;
int pos = 0;
unordered_map<Node*, int > m;
while (p != NULL) {
if (m.find(p) == m.end()) {
m[p] = pos;
pos++;
}
else {
return (pos - m[p]);
}
p = p->next;
}
return 0;
}
int main()
{
struct Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
head->next->next->next->next->next = head->next;
cout << countNodesinLoop(head) << endl;
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static class Node
{
int data;
Node next;
Node( int num)
{
data = num;
next = null ;
}
}
public static int countNodesinLoop(Node head)
{
Node p = head;
int pos = 0 ;
HashMap<Node,
Integer> m = new HashMap<Node,
Integer>();
while (p != null )
{
if (!m.containsKey(p))
{
m.put(p, pos);
pos++;
}
else
{
return (pos - m.get(p));
}
p = p.next;
}
return 0 ;
}
public static void main (String[] args)
{
Node head = new Node( 1 );
head.next = new Node( 2 );
head.next.next = new Node( 3 );
head.next.next.next = new Node( 4 );
head.next.next.next.next = new Node( 5 );
head.next.next.next.next.next = head.next;
System.out.println(countNodesinLoop(head));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def countNodesinLoop(head):
p = head;
pos = 0 ;
m = dict ()
while (p ! = None ):
if (p not in m):
m[p] = pos;
pos + = 1
else :
return (pos - m[p]);
p = p. next ;
return 0 ;
if __name__ = = '__main__' :
head = Node( 1 );
head. next = Node( 2 );
head. next . next = Node( 3 );
head. next . next . next = Node( 4 );
head. next . next . next . next = Node( 5 );
head. next . next . next . next . next = head. next ;
print (countNodesinLoop(head))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
public class Node
{
public int data;
public Node next;
public Node( int num)
{
data = num;
next = null ;
}
}
public static int countNodesinLoop(Node head)
{
Node p = head;
int pos = 0;
Dictionary<Node,
int > m = new Dictionary<Node,
int >();
while (p != null )
{
if (!m.ContainsKey(p))
{
m[p] = pos;
pos++;
}
else
{
return (pos - m[p]);
}
p = p.next;
}
return 0;
}
public static void Main( string [] args)
{
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = head.next;
Console.Write(countNodesinLoop(head));
}
}
|
Javascript
<script>
class Node {
constructor(num)
{
this .data = num;
this .next = null ;
}
};
function countNodesinLoop(head)
{
var p = head;
var pos = 0;
var m = new Map();
while (p != null ) {
if (!m.has(p)) {
m.set(p, pos);
pos++;
}
else {
return (pos - m.get(p));
}
p = p.next;
}
return 0;
}
var head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = head.next;
document.write( countNodesinLoop(head));
</script>
|
Time Complexity: O(n) where n is size of linked list
Auxiliary Space: O(n)
Similar Article: Find the length of a loop in a Linked list using Floyd’s Cycle detection algorithm
Last Updated :
23 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...