题目链接:https://leetcode.cn/problems/linked-list-cycle-ii/
难度:中等
描述:
给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 _null_。
题解
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution:def detectCycle(self, head: ListNode) -> ListNode:slow = fast = headwhile fast and fast.next:slow = slow.nextfast = fast.next.nextif slow == fast:fast = headwhile slow != fast:fast = fast.nextslow = slow.nextreturn fastreturn None
