leetcode 141. 环形链表(python)

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

快慢指针法:
忘记了 参考万门视频。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        slow = fast = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
            if slow == fast:
                return True
        return False
发布了107 篇原创文章 · 获赞 56 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/weixin_41504611/article/details/104728546