3. 141 环形链表

3. 141 环形链表

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:
        """
        快慢指针,如果快指针和慢指针不相遇就不是环
        """
        fast = slow = head
        while fast and fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow is fast:
                return True
        return False
        

Guess you like

Origin blog.csdn.net/weixin_39754630/article/details/116013629