Detect Cycle in a Linked List using Map
Given a Linked List, check if the linked list has a loop or not.
There are various methods shown here: Detect Cycle in Linked List
Example
Input: 20->4->54->6->NULL
Output: No loop is detected.
Explanation:
While traversing the linked list, we reach the end of the linked list. Therefore, no loop is present in the linked list.
Input: 20->4->5->10->20
Output: Loop detected.
Explanation:
While traversing the linked list, reaching the node with value 10, it is linked with the head node, which depicts a loop in the linked list. Therefore, a loop is present in the linked list.
Approach:
- Create a map that will store the visited node in the linked list.
- Traverse the linked list and do the following:
- Check whether the current node is present on the map or not.
- If the current node is not present in the map then, insert the current node into the map.
- If the Node is present in the map, the loop in a linked list is detected.
- If we reach the Null Node while traversing the linked list then, the given linked list has no loop present in it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* newNode( int d)
{
Node* temp = new Node;
temp->data = d;
temp->next = NULL;
return temp;
}
map<Node*, bool > vis;
bool flag = 0;
void check(Node* head)
{
if (head == NULL) {
flag = 0;
return ;
}
if (!vis[head]) {
vis[head] = true ;
check(head->next);
}
else {
flag = 1;
return ;
}
}
int main()
{
Node* head = newNode(20);
head->next = newNode(4);
head->next->next = newNode(5);
head->next->next->next = newNode(10);
head->next->next->next->next = head;
check(head);
if (flag)
cout << "Loop detected." ;
else
cout << "No Loop Found." ;
cout << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node
{
int data;
Node next;
};
static Node newNode( int d)
{
Node temp = new Node();
temp.data = d;
temp.next = null ;
return temp;
}
static HashMap<Node, Boolean> vis = new HashMap<>();
static boolean flag = false ;
static void check(Node head)
{
if (head == null )
{
flag = false ;
return ;
}
if (vis.containsKey(head))
{
vis.put(head, true );
check(head.next);
}
else
{
flag = true ;
return ;
}
}
public static void main(String[] args)
{
Node head = newNode( 20 );
head.next = newNode( 4 );
head.next.next = newNode( 5 );
head.next.next.next = newNode( 10 );
head.next.next.next.next = head;
check(head);
if (flag)
System.out.print( "Loop detected." );
else
System.out.print( "No Loop Found." );
System.out.println();
}
}
|
Python3
class Node:
def __init__( self , val):
self .data = val
self . Next = None
def newNode(d):
temp = Node(d)
return temp
vis = {}
flag = False
def check(head):
global vis, flag
if head = = None :
flag = False
return
if head not in vis:
vis[head] = True
check(head. Next )
else :
flag = True
return
head = newNode( 20 )
head. Next = newNode( 4 )
head. Next . Next = newNode( 5 )
head. Next . Next . Next = newNode( 10 )
head. Next . Next . Next . Next = head
check(head)
if flag:
print ( "Loop detected." )
else :
print ( "No Loop Found." )
|
C#
using System;
using System.Collections.Generic;
class GFG{
public class Node
{
public int data;
public Node next;
};
static Node newNode( int d)
{
Node temp = new Node();
temp.data = d;
temp.next = null ;
return temp;
}
static Dictionary<Node,
Boolean> vis = new Dictionary<Node,
Boolean>();
static bool flag = false ;
static void check(Node head)
{
if (head == null )
{
flag = false ;
return ;
}
if (vis.ContainsKey(head))
{
vis.Add(head, true );
check(head.next);
}
else
{
flag = true ;
return ;
}
}
public static void Main(String[] args)
{
Node head = newNode(20);
head.next = newNode(4);
head.next.next = newNode(5);
head.next.next.next = newNode(10);
head.next.next.next.next = head;
check(head);
if (flag)
Console.Write( "Loop detected." );
else
Console.Write( "No Loop Found." );
Console.WriteLine();
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function newNode(d) {
var temp = new Node();
temp.data = d;
temp.next = null ;
return temp;
}
var vis = new Map();;
var flag = false ;
function check(head) {
if (head == null ) {
flag = false ;
return ;
}
if (!vis.has(head)) {
vis.set(head, true );
check(head.next);
}
else {
flag = true ;
return ;
}
}
var head = newNode(20);
head.next = newNode(4);
head.next.next = newNode(5);
head.next.next.next = newNode(10);
head.next.next.next.next = head;
check(head);
if (flag)
document.write( "Loop detected." );
else
document.write( "No Loop Found." );
document.write();
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
09 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...