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
  • 相关阅读:
    全网数据库大全
    Docker最全教程
    枚举策略
    POJ 1012
    阿里云轻量应用服务器入门(二)
    阿里云轻量应用服务器入门(一)
    云服务器ECS
    Codeforces 768B B. Code For 1
    链剖&LCT总结
    O(1) 查询gcd
  • 原文地址:https://www.cnblogs.com/rosyYY/p/10998040.html
Copyright © 2011-2022 走看看