linked_list_cycle

  • 2022-12-14
  • 浏览 (596)

linked_list_cycle.py 源码

# 判断链表是否有环
# https://leetcode-cn.com/problems/linked-list-cycle

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:

    # 使用一个set
    def hasCycle_1(self, head: ListNode) -> bool:
        s = set()
        while head:
            if head in s:
                return True
            s.add(head)
            head = head.next
        return False

    # 快慢指针
    def hasCycle_2(self, head: ListNode) -> bool:
        slow, fast = head, head
        while fast and fast.next:
            slow, fast = slow.next, fast.next.next
            if slow == fast:
                return True
        return False

你可能感兴趣的文章

add_two_numbers

delete_nth_node

intersection_of_two_linked_lists

0  赞