BM6 判断链表中是否有环
This commit is contained in:
parent
f49dc89756
commit
58bff4bc53
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user