LeetCode题目--环形链表(python实现)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28266311/article/details/83024265

题目

给定一个链表,判断链表中是否有环。

进阶:
你能否不使用额外空间解决此题?

python代码实现:

# 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

        

猜你喜欢

转载自blog.csdn.net/qq_28266311/article/details/83024265
今日推荐