-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetect.java
More file actions
60 lines (60 loc) · 1.22 KB
/
Detect.java
File metadata and controls
60 lines (60 loc) · 1.22 KB
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
51
52
53
54
55
56
57
58
59
60
import java.util.*;
public class Detect
{
static class Node
{
int data;
Node next;
Node(int d)
{
data=d;
}
}
static boolean Loop(Node head)
{
Node slow=head,fast=head;
while(fast!=null && fast.next!=null)
{
slow=slow.next;
fast=fast.next.next;
if(slow==fast){
Node start=head;
while(start!=slow)
{
start=start.next;
slow=slow.next;
}
Node ptr=start;
while(ptr.next!=start)
ptr=ptr.next;
ptr.next=null;
return true;
}
}
return true;
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
if(n<=0||n>100000){
System.out.println("Invalid node count");
return;
}
Node head=new Node(in.nextInt());
Node tail=head;
List<Node> nodes=new ArrayList<>();
nodes.add(head);
for(int i=1;i<n;i++)
{
Node node=new Node(in.nextInt());
tail.next=node;
tail=node;
nodes.add(node);
}
int pos=in.nextInt();
if(pos>0)
tail.next=nodes.get(pos-1);
System.out.println("Output:"+Loop(head));
}
}