[leetcode142]Linked List Cycle II

xiaoxiao2021-02-28  63

# Definition for singly-linked list. # class ListNode(object): #     def __init__(self, x): #         self.val = x #         self.next = None class Solution(object):     def detectCycle(self, head):         """         :type head: ListNode         :rtype: ListNode         """         slow=fast=head         while fast and fast.next:             slow=slow.next             fast=fast.next.next             if id(slow)==id(fast):                 fast=head                 while fast!=slow:                     fast=fast.next                     slow=slow.next                 return slow

        return None

转载请注明原文地址: https://www.6miu.com/read-59957.html

最新回复(0)