美文网首页
141. Linked List Cycle 快慢指针经典应用

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

作者: 羲牧 | 来源:发表于2020-05-30 23:39 被阅读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
    

    相关文章

      网友评论

          本文标题:141. Linked List Cycle 快慢指针经典应用

          本文链接:https://www.haomeiwen.com/subject/kyvkzhtx.html