zoukankan      html  css  js  c++  java
  • 【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

  • 相关阅读:
    day17---无参装饰器
    day17---作业
    python面向过程的编程思想
    算法之二分法
    python递归函数
    pyth作业3/25
    三元表达式、生成式、生成器表达式
    python 3/24作业
    python生成器
    python 迭代器
  • 原文地址:https://www.cnblogs.com/kwangeline/p/5953519.html
Copyright © 2011-2022 走看看