【leetcode❤python】141. Linked List Cycle

#-*- coding: UTF-8 -*-
#Method:快慢指针法,建立虚表头,快指针走两步,慢指针走一步,若存在环,则快指针会追上慢指针
# 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
        """
        if head.next==None or head==None or head.next.next==None:
            return False
        dummy=listNode(0)
        dummy.next=head
        fast=dummy
        slow=dummy
        while pre!=None and cur!=None and cur.next!=None:
            
            slow=slow.next
            fast=fast.next.next
            if pre==cur:
                return True
        return False

原文地址:https://www.cnblogs.com/kwangeline/p/5953519.html