141. Linked List Cycle 快慢指针经典应用

2020-05-30  本文已影响0人  羲牧
# 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:
        if head is None or head.next is None:
            return False
        p = head
        q = head
        while q and q.next:
            p = p.next
            q = q.next.next
            if p == q:
                return True
        return False
上一篇下一篇

猜你喜欢

热点阅读