Why is my solution exceeding "Memory limit" ?


  • 0
    M

    I have the following simple solution implemented : but it seems to exceed memory limits for some reason , can someone take a look see whats wrong ?

    class Solution(object):
        def getIntersectionNode(self, headA, headB):
            """
            :type head1, head1: ListNode
            :rtype: ListNode
            """
            if headA == None or headB ==None:
                return None
            else:
                current_A , current_B = headA,headB
                count_A,count_B = 1,1
                while current_A.next:
                    current_A = current_A.next
                    count_A +=1
    
                while current_B.next:
                    current_B = current_B.next
                    count_B +=1
    
                current_A , current_B = headA,headB
                diff = abs(count_A - count_B)
                print count_A , count_B
                if count_A > count_B:
                    for i in xrange(diff):
                        current_A = current_A.next
                    while current_A.next and current_B.next:
                        if current_A == current_B:
                            return current_A
                        else:
                            current_A = current_A.next
                            current_B = current_B.next
                        return None
                elif count_B > count_A :
                    for i in xrange(diff):
                        current_B = current_B.next
                    while current_A.next and current_B.next:
                        if current_A == current_B:
                            return current_A
                        else:
                            current_A = current_A.next
                            current_B = current_B.next
                        return None

  • 0
    W

    I get memory limit too, with code somehow like yours. But I've tried simply coping every answer in LeetCode Discuss written in Python and all those got memory limit exceeded.
    Dunno why.
    Someone please help.

    Updated 06/20/2016:
    Wired thing happened. I first run a solution and it got memory limit.
    Then I tried copying another solution. It passed! And when I used ctrl+z to get back to my first solution and tried submitting it again, it got passed too.

    There must be something wrong.


  • 0
    E

    I have the same situation today, I am using Python as well.


  • 0
    Y

    I have written similar code in Python, and my code also exceeds the memory limit. Can anyone spot where memory corruption may have occurred? Thanks!

    class Solution(object):
     def getIntersectionNode(self, headA, headB):
            """
            :type head1, head1: ListNode
            :rtype: ListNode
            """
            if headA is None or headB is None:
                return None
            curr_A = headA
            curr_B = headB
            A_len = self.getListLength(curr_A)
            B_len = self.getListLength(curr_B)
            diff = A_len - B_len
            if diff > 0:
                for i in range(diff):
                    curr_A = curr_A.next
            else:
                for j in range(-diff):
                    curr_B = curr_B.next
            while curr_A is not None and curr_B is not None:
                if curr_A.val == curr_B.val:
                    return curr_A
                else:
                    curr_A = curr_A.next
                    curr_B  = curr_B.next
            return None
    
    def getListLength(self, head):
            curr = head
            node_count = 1
            while curr.next is not None:
                node_count += 1
                curr = curr.next
            return node_count
    

Log in to reply
 

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