Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 83a7a0b

Browse files
add
1 parent 36eba57 commit 83a7a0b

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package LeetcodeQuestions;
2+
3+
class cNode{
4+
5+
int data;
6+
cNode next;
7+
public cNode(int data){
8+
this.data=data;
9+
}
10+
11+
}
12+
13+
14+
15+
public class detectCircleLL {
16+
17+
// func to detect circular
18+
public static boolean detect(cNode head){
19+
20+
21+
cNode fast=head;
22+
cNode slow=head;
23+
24+
while(fast!=null && fast.next!=null){
25+
26+
slow=slow.next;
27+
fast=fast.next.next;
28+
29+
if(slow==fast){
30+
31+
return true;
32+
}
33+
34+
35+
36+
}
37+
38+
return false;
39+
40+
}
41+
42+
public static cNode startPoint(cNode head){
43+
44+
cNode fast=head;
45+
cNode slow=head;
46+
boolean flag=false;
47+
48+
while(fast!=null && fast.next!=null){
49+
50+
slow=slow.next;
51+
fast=fast.next.next;
52+
53+
if(slow==fast){
54+
55+
flag=true;
56+
break;
57+
}
58+
}
59+
60+
if(flag==false){
61+
return null;
62+
}
63+
64+
fast=head;
65+
66+
67+
while(slow!=fast){
68+
69+
slow=slow.next;
70+
fast=fast.next;
71+
}
72+
73+
return slow;
74+
75+
}
76+
77+
78+
79+
80+
81+
82+
public static void main(String[] args) {
83+
84+
cNode head=new cNode(3);
85+
head.next=new cNode(2);
86+
head.next.next=new cNode(1);
87+
head.next.next=head;
88+
89+
System.out.println(startPoint(head).data);
90+
System.out.println(detect(head));
91+
92+
}
93+
94+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /