-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathT10.java
More file actions
35 lines (30 loc) · 828 Bytes
/
T10.java
File metadata and controls
35 lines (30 loc) · 828 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
package testCode;
public class T10 {
static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public static final void main(String args[]) {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = head;
System.out.print(hasCycle(head));
}
public static boolean hasCycle(ListNode head) {
ListNode slowPtr = head;
ListNode fastPtr = head;
while (fastPtr != null && fastPtr.next != null) {
slowPtr = slowPtr.next;
fastPtr = fastPtr.next.next;
if (slowPtr == fastPtr) {
return true;
}
}
return false;
}
}