边工作边刷题:70天一遍leetcode: day 10
Linked List Cycle I/II
fast/slow指针的移动方式:fast先移动,然后检查fast again,同时移动fast和slow
# 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
"""
fast = slow = head
while fast:
fast=fast.next
if fast:
fast=fast.next
slow=slow.next
if fast==slow:
return True
return False