Share python with explanation


  • 0
    class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:return None
        p,q=head,head
        while p and q:
            if p.next:p=p.next
            else:return None
            if q.next and q.next.next:q=q.next.next
            else:return None
            if p==q:break
        r=head
        while p!=r:
            p=p.next
            r=r.next
        return r
    

    the main idea is come from this blog http://www.cnblogs.com/snake-hand/p/3148328.html.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.