美文网首页
141. Linked List Cycle

141. Linked List Cycle

作者: 7ccc099f4608 | 来源:发表于2020-03-17 13:45 被阅读0次

https://leetcode-cn.com/problems/linked-list-cycle/

image.png

(图片来源https://leetcode-cn.com/problems/linked-list-cycle/

日期 是否一次通过 comment
2020-03-17 0

递归

public boolean hasCycle(ListNode pHead) {
        if(pHead == null || pHead.next == null) {
            return false;
        }
        
        ListNode pSlow = pHead;
        ListNode pFast = pHead;
        
        while(pFast != null && pFast.next != null) {
            pSlow = pSlow.next;
            pFast = pFast.next.next;
            
            if(pSlow == pFast) {
                return true;
            }
        }
        
        return false;  
    }

相关文章

网友评论

      本文标题:141. Linked List Cycle

      本文链接:https://www.haomeiwen.com/subject/cbktyhtx.html