题解 | #判断链表中是否有环#

判断链表中是否有环

http://www.nowcoder.com/practice/650474f313294468a4ded3ce0f7898b9

快慢指针

    def hasCycle(self , head: ListNode) -> bool:
        if not head or not head.next:
            return False
        slow = head
        fast = head
        
        while fast:
            try:
                fast = fast.next.next
                slow=slow.next
                if fast == slow:
                    return True
            except:
                return False
        return False
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务