BM6 判断链表中是否有环

This commit is contained in:
轩辕龙儿 2022-07-26 16:58:42 +08:00
parent f49dc89756
commit 58bff4bc53

View File

@ -0,0 +1,28 @@
package com.huangge1199.nowcoder.bm.BM6;
import com.huangge1199.nowcoder.common.ListNode;
/**
* @author hyy
* @Classname Solution
* @Description BM6 判断链表中是否有环
* @Date 2022/7/26 16:58
*/
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast != null) {
fast = fast.next;
if (fast == null) {
return false;
}
fast = fast.next;
slow = slow.next;
if (fast == slow) {
return true;
}
}
return false;
}
}