-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDetectLoop.java
50 lines (47 loc) · 1.27 KB
/
DetectLoop.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Given a singly linked list, detect whether the given LL has a loop.
import java.util.*;
class LinkedList
{
static Node head; // head of list
/* Linked list Node*/
static class Node
{
int data;
Node next;
// Constructor to create a new node
// Next is by default initialized
// as null
Node(int d) {
data = d;
next=null;
}
}
public static void insertNode(int item){
Node node = new Node(item);
node.next = head;
head = node;
}
public static boolean detectLoop(Node x){
HashSet<Node> s = new HashSet<Node>();
while(x != null){
if(s.contains(x))
return true; // if node is already present,then a loop exists
s.add(x);
x = x.next;
}
return false;
}
public static void main(String args[]){
LinkedList llist = new LinkedList();
llist.insertNode(1);
llist.insertNode(2);
llist.insertNode(3);
llist.insertNode(4);
llist.insertNode(5);
llist.head.next.next = llist.head;
if(detectLoop(head))
System.out.println("Loop found");
else
System.out.println("No loop :)");
}
}