-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindCycle.java
More file actions
47 lines (37 loc) · 977 Bytes
/
Copy pathFindCycle.java
File metadata and controls
47 lines (37 loc) · 977 Bytes
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
public class FindCycle {
public static void main(String[] args) {
FindCycle c = new FindCycle();
ListNode head = new ListNode(1);
head.next = new ListNode(1);
head.next.next = head;
System.out.println(c.hasCycle(head));
}
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public boolean hasCycle(ListNode head) {
if(head == null)
return false;
ListNode temp = head;
while(head != null && temp != null)
{
if(head.next != null)
head = head.next;
else
return false;
if(temp.next != null && temp.next.next != null)
temp = temp.next.next;
else
return false;
if(head == temp)
return true;
//1 2 3 4 5 6
}
return false;
}
}