zoukankan      html  css  js  c++  java
  • leetcode-easy-listnode-141 Linked List Cycle

    mycode  98.22%

    # 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 not head or not head.next:
                return False
            fast = slow = head
            while fast.next and fast.next.next:
                slow = slow.next
                fast = fast.next.next
                if  slow == fast:
                    return True
            return False

    参考

    可以稍微简化一丢丢

    # 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
            """
            slow = fast = head
            while fast and fast.next:
                slow = slow.next
                fast = fast.next.next
                if slow == fast:
                    return True
            return False
  • 相关阅读:
    char
    export和export default
    递归打印文件目录
    centso7 安装redmine
    sonar结合jenkins
    sonar安装
    gitlab+jenkins
    centos5 安装redmine
    elk安装最佳实践
    elk认证模块x-pack安装
  • 原文地址:https://www.cnblogs.com/rosyYY/p/10998040.html
Copyright © 2011-2022 走看看