# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
try:
slow,fast=head,head.next
while slow is not fast:
slow,fast=slow.next,fast.next.next
return True
except:
return False
141. Linked List Cycle
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 问题描述 Given a linked list, determine if it has a cycle in ...
- Given a linked list, determine if it has a cycle in it.给定...
- Given a linked list, determine if it has a cycle in it.Fo...
- 这题从前校招的时候看面试宝典的时候就见到过。思想就是快慢指针。 Use two pointers, walker ...