题目

题解
思路:floyd判圈法;
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
fast, slow = head, head
while True:
if not (fast and fast.next):
return
fast = fast.next.next
slow = slow.next
if fast == slow:
break
fast = head
while fast != slow:
fast = fast.next
slow = slow.next
return fast
本文介绍了一种使用Floyd算法检测链表中环的解决方案,通过快慢指针技巧巧妙地找到链表是否存在循环,并定位循环起点。
344

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



