O(1) space Python got MLE but accepted before


  • 0

    I don't know why this will lead to MLE. Only O(1) space is used from my aspect. This code was accepted 3 month ago and beats 99%.

    class Solution(object):
        def getIntersectionNode(self, headA, headB):
            """
            :type head1, head1: ListNode
            :rtype: ListNode
            """
            pa = headA
            pb = headB
            la = lb = 0
            while pa:
                la += 1
                pa = pa.next
            while pb:
                lb += 1
                pb = pb.next
                        
            step = abs(la - lb)
            if la > lb:
                while step:
                    headA = headA.next
                    step -= 1
            else:
                while step:
                    headB = headB.next
                    step -= 1
            while headA and headB:
                if headA == headB:
                    return headA
                headA = headA.next
                headB = headB.next
    

  • 0

    Now I think this is a problem of OJ because the most voted answer cannot get accpet.


Log in to reply
 

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