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

//快慢指针
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; } }

  

posted on 2023-07-20 16:32  Hi,Bro  阅读(2)  评论(0编辑  收藏  举报