# 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
"""
if not head or not head.next:
return False
fast=head
slow=head
while(fast.next and fast.next.next):
fast=fast.next.next
slow=slow.next
if slow.val==fast.val:
return True
return False
本文介绍了一种使用快慢指针技术检测单链表中是否存在循环的有效算法。通过实例讲解了算法的具体实现过程,以及如何判断链表节点是否形成闭环。
415

被折叠的 条评论
为什么被折叠?



