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

  • 相关阅读:
    iOS进阶二-KVC
    iOS进阶一OC对象的本质
    2019-01-19
    2019-01-12
    2019
    2018-12-23 随笔
    2018-12-18 随笔
    2018-12-10
    2018-12-01
    2018.11.23 随笔
  • 原文地址:https://www.cnblogs.com/kwangeline/p/5953519.html
Copyright © 2011-2022 走看看