141.Linked List Cycle (判断一个单链表是否有环)

来源:互联网 发布:冰箱品牌知乎 编辑:程序博客网 时间:2024/05/18 20:49
public class Solution {
    public boolean hasCycle(ListNode head) {
     ListNode slow = head, fast = head;
    
      while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
    
        if (slow == fast) 
            return true;
      }
    
      return false;
    }
}
0 0