Leet Code 141 Linked List Cycle

判断链表是否有环

使用快慢双指针,当两个指针重合时即存在环;
当快指针指向None时,不存在环。
快慢指针也可以用于找链表的中点,删除倒数第k个节点等。

# 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
        """
        fast = slow = head
        while fast and slow and fast.next:
            fast = fast.next.next
            slow = slow.next
            if fast is slow:
                return True
        return False

猜你喜欢

转载自blog.csdn.net/zz_daisy/article/details/89844408