上班摸鱼刷算法-Java-hot100-[141]环形链表

发布时间 2023-07-20 16:38:52作者: Hi,Bro
//快慢指针
public class Solution { public boolean hasCycle(ListNode head) { if (head == null || head.next == null) { return false; } ListNode fastNode = head.next; ListNode slowNode = head; while (fastNode != slowNode) { if (fastNode == null || fastNode.next == null) { return false; } fastNode = fastNode.next.next; slowNode = slowNode.next; } return true; } }