# Detect cycle in a linked list in linear time

Here given code implementation process.

``````/*
Java program
Detect cycle in a linked list in linear time
*/
import java.util.Set;
import java.util.HashSet;

{
public int data;
{
this.data = data;
this.next = null;
}
}

public class LoopDetection
{
// Check whether given linked list contains loop or not
{
// Use to collect unique nodes

// Loop indicators
Boolean status = false;

while (node != null && status == false)
{
if (result.contains(node))
{
status = true;
}
else
{
// Display node value
System.out.print("  " + (node.data));
}
// Visit to next node
node = node.next;
}
if (status == true)
{
System.out.print("\n  Loop are exist\n");
}
else
{
System.out.print("\n  Loop are not exist\n");
}
}
public static void main(String[] args)
{

// 1 → 2 → 3 → 4 → 5 → 6 → NULL
// Create a loop

// 1 → 2 → 3 → 4 → 5 → 6 ──┐
//         └───────────────┘
// Test
}
}``````

#### Output

``````  1  2  3  4  5  6
Loop are not exist
1  2  3  4  5  6
Loop are exist``````
``````// Include header file
#include <iostream>
#include <set>
using namespace std;

{
public: int data;
{
this->data = data;
this->next = NULL;
}
};
class LoopDetection
{
public:
// Check whether given linked list contains loop or not
{
// Use to collect unique nodes
// Loop indicators
bool status = false;
while (node != NULL && status == false)
{
if (result.find(node) != result.end())
{
status = true;
}
else
{
// Display node value
cout << "  " << (node->data);
result.insert(node);
}
// Visit to next node
node = node->next;
}
if (status == true)
{
cout << "\n  Loop are exist\n";
}
else
{
cout << "\n  Loop are not exist\n";
}
}
};
int main()
{
// 1 → 2 → 3 → 4 → 5 → 6 → NULL
// Create a loop
// 1 → 2 → 3 → 4 → 5 → 6 ──┐
//         └───────────────┘
// Test
return 0;
}``````

#### Output

``````  1  2  3  4  5  6
Loop are not exist
1  2  3  4  5  6
Loop are exist``````
``````// Include namespace system
using System;
using System.Collections.Generic;
/*
C# program
Detect cycle in a linked list in linear time
*/
{
public int data;
{
this.data = data;
this.next = null;
}
}
public class LoopDetection
{
// Check whether given linked list contains loop or not
{
// Use to collect unique nodes
HashSet < LinkNode > result = new HashSet < LinkNode > ();
// Loop indicators
Boolean status = false;
while (node != null && status == false)
{
if (result.Contains(node))
{
status = true;
}
else
{
// Display node value
Console.Write("  " + (node.data));
}
// Visit to next node
node = node.next;
}
if (status == true)
{
Console.Write("\n  Loop are exist\n");
}
else
{
Console.Write("\n  Loop are not exist\n");
}
}
public static void Main(String[] args)
{
// 1 → 2 → 3 → 4 → 5 → 6 → NULL
// Create a loop
// 1 → 2 → 3 → 4 → 5 → 6 ──┐
//         └───────────────┘
// Test
}
}``````

#### Output

``````  1  2  3  4  5  6
Loop are not exist
1  2  3  4  5  6
Loop are exist``````
``````<?php
/*
Php program
Detect cycle in a linked list in linear time
*/
{
public \$data;
public \$next;

function __construct(\$data)
{
\$this->data = \$data;
\$this->next = null;
}
}
class LoopDetection
{
// Check whether given linked list contains loop or not
{
// Use to collect unique nodes
\$result = array();
// Loop indicators
\$status = false;
while (\$node != null && \$status == false)
{
if ((\$key = array_search(\$node, \$result) !== false))
{
\$status = true;
}
else
{
// Display node value
echo "  ". (\$node->data);
\$result[] = \$node;
}
// Visit to next node
\$node = \$node->next;
}
if (\$status == true)
{
echo "\n  Loop are exist\n";
}
else
{
echo "\n  Loop are not exist\n";
}
}
}

function main()
{
// 1 → 2 → 3 → 4 → 5 → 6 → NULL
// Create a loop
// 1 → 2 → 3 → 4 → 5 → 6 ──┐
//         └───────────────┘
// Test
}
main();``````

#### Output

``````  1  2  3  4  5  6
Loop are not exist
1  2  3  4  5  6
Loop are exist``````
``````/*
Node Js program
Detect cycle in a linked list in linear time
*/
{
constructor(data)
{
this.data = data;
this.next = null;
}
}
class LoopDetection
{
// Check whether given linked list contains loop or not
{
// Use to collect unique nodes
var result = new Set();
// Loop indicators
var status = false;
while (node != null && status == false)
{
if (result.has(node))
{
status = true;
}
else
{
// Display node value
process.stdout.write("  " + (node.data));
}
// Visit to next node
node = node.next;
}
if (status == true)
{
process.stdout.write("\n  Loop are exist\n");
}
else
{
process.stdout.write("\n  Loop are not exist\n");
}
}
}

function main()
{
// 1 → 2 → 3 → 4 → 5 → 6 → NULL
// Create a loop
// 1 → 2 → 3 → 4 → 5 → 6 ──┐
//         └───────────────┘
// Test
}
main();``````

#### Output

``````  1  2  3  4  5  6
Loop are not exist
1  2  3  4  5  6
Loop are exist``````
``````#   Python 3 program
#   Detect cycle in a linked list in linear time

def __init__(self, data) :
self.data = data
self.next = None

class LoopDetection :
#  Check whether given linked list contains loop or not
#  Use to collect unique nodes
result = set()
#  Loop indicators
status = False
while (node != None and status == False) :
if (node in result) :
status = True
else :
#  Display node value
print(" ", (node.data), end = "")

#  Visit to next node
node = node.next

if (status == True) :
print("\n  Loop are exist")
else :
print("\n  Loop are not exist")

def main() :
#  1 → 2 → 3 → 4 → 5 → 6 → NULL
#  Create a loop
#  1 → 2 → 3 → 4 → 5 → 6 ──┐
#          └───────────────┘
#  Test

if __name__ == "__main__": main()``````

#### Output

``````  1  2  3  4  5  6
Loop are not exist
1  2  3  4  5  6
Loop are exist``````
``````#   Ruby program
#   Detect cycle in a linked list in linear time

attr_accessor :data, :next

def initialize(data)
self.data = data
self.next = nil
end
end

class LoopDetection
#  Check whether given linked list contains loop or not
#  Use to collect unique nodes
result = []
#  Loop indicators
status = false
while (node != nil && status == false)
if (result.include?(node))
status = true
else
#  Display node value
print("  ", (node.data))
result.push(node)
end

#  Visit to next node
node = node.next
end

if (status == true)
print("\n  Loop are exist\n")
else
print("\n  Loop are not exist\n")
end

end

end

def main()
#  1 → 2 → 3 → 4 → 5 → 6 → NULL
#  Create a loop
#  1 → 2 → 3 → 4 → 5 → 6 ──┐
#          └───────────────┘
#  Test
end

main()``````

#### Output

``````  1  2  3  4  5  6
Loop are not exist
1  2  3  4  5  6
Loop are exist
``````
``````import scala.collection.mutable._;
/*
Scala program
Detect cycle in a linked list in linear time
*/
{
def this(data: Int)
{
this(data, null);
}
}
class LoopDetection
{
// Check whether given linked list contains loop or not
// Use to collect unique nodes
// Loop indicators
var status: Boolean = false;
while (node != null && status == false)
{
if (result.contains(node))
{
status = true;
}
else
{
// Display node value
print("  " + (node.data));
}
// Visit to next node
node = node.next;
}
if (status == true)
{
print("\n  Loop are exist\n");
}
else
{
print("\n  Loop are not exist\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: LoopDetection = new LoopDetection();
// 1 → 2 → 3 → 4 → 5 → 6 → NULL
// Create a loop
// 1 → 2 → 3 → 4 → 5 → 6 ──┐
//         └───────────────┘
// Test
}
}``````

#### Output

``````  1  2  3  4  5  6
Loop are not exist
1  2  3  4  5  6
Loop are exist``````
``````/*
Swift 4 program
Detect cycle in a linked list in linear time
*/
{
var data: Int;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
}
class LoopDetection
{
// Check whether given linked list contains loop or not
{
// Use to collect unique nodes
// Loop indicators
var status: Bool? = false;
while (node  != nil && status == false)
{
if (result.contains(where: {\$0 === node}))
{
status = true;
}
else
{
// Display node value
print("  ", (node!.data), terminator: "");
result.append(node!);
}
// Visit to next node
node = node!.next;
}
if (status == true)
{
print("\n  Loop are exist");
}
else
{
print("\n  Loop are not exist");
}
}
}
func main()
{
// 1 → 2 → 3 → 4 → 5 → 6 → NULL
// Create a loop
// 1 → 2 → 3 → 4 → 5 → 6 ──┐
//         └───────────────┘
// Test
}
main();``````

#### Output

``````   1   2   3   4   5   6
Loop are not exist
1   2   3   4   5   6
Loop are exist``````
``````/*
Kotlin program
Detect cycle in a linked list in linear time
*/
{
var data: Int;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
}
class LoopDetection
{
// Check whether given linked list contains loop or not
{
// Use to collect unique nodes
// Loop indicators
var status: Boolean ? = false;
while (node != null && status == false)
{
if (result.contains(node))
{
status = true;
}
else
{
// Display node value
print("  " + (node.data));
}
// Visit to next node
node = node.next;
}
if (status == true)
{
print("\n  Loop are exist\n");
}
else
{
print("\n  Loop are not exist\n");
}
}
}
fun main(args: Array < String > ): Unit
{
// 1 → 2 → 3 → 4 → 5 → 6 → NULL
// Create a loop
// 1 → 2 → 3 → 4 → 5 → 6 ──┐
//         └───────────────┘
// Test
}``````

#### Output

``````  1  2  3  4  5  6
Loop are not exist
1  2  3  4  5  6
Loop are exist``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.