leetcode python3 环形链表

代码思路:利用快慢指针遍历链表,若存在环形链表,两指针必相遇,否则快指针会先遍历完成

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        show=fast=head
        while fast and fast.next:
            show=show.next
            fast=fast.next.next
            if show==fast:
                return True
        return False
发布了30 篇原创文章 · 获赞 0 · 访问量 321

猜你喜欢

转载自blog.csdn.net/m0_37656366/article/details/104745759