-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_141.java
More file actions
34 lines (31 loc) · 946 Bytes
/
a_141.java
File metadata and controls
34 lines (31 loc) · 946 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
package doublepointer;
public class a_141 {
/*public boolean hasCycle(ListNode head) {
if (head == null) return false;
ListNode i = head, j = head.next;
while(j != null) {
if (i == j) return true;
j = j.next;
}
return false;
}*/ //错误解法
//双指针,一个指针每次移动一个节点,一个指针每次移动二个节点,如果存在环,那么这两个指针一定会相遇。
public boolean hasCycle(ListNode head) {
if (head == null) return false;
ListNode i = head, j = head.next;
while (i != null && j != null && j.next != null) {
if (i == j) return true;
i = i.next;
j = j.next.next;
}
return false;
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
}