首页 > 其他 > 详细

Leetcode 142. Linked List Cycle II

时间:2019-04-17 23:45:47      阅读:144      评论:0      收藏:0      [点我收藏+]

龟兔赛跑算法

# 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
        """
        try:
            slow = head
            fast = head.next
            while slow is not fast:
                slow = slow.next
                fast = fast.next.next
            slow=head.next
            while slow is not fast:
                slow=slow.next
                fast=fast.next
            return slow
            
        except:
            return

 

Leetcode 142. Linked List Cycle II

原文:https://www.cnblogs.com/zywscq/p/10726882.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!