[Python] Solved for memory limit problem by adding gc.collect()


  • -1
    F

    '''

    Definition for singly-linked list.

    class ListNode(object):

    def init(self, x):

    self.val = x

    self.next = None

    import gc
    class Solution(object):
    def getIntersectionNode(self, headA, headB):
    """
    :type head1, head1: ListNode
    :rtype: ListNode
    """
    if not headA or not headB:
    return None
    curA=headA
    curB=headB
    gc.collect()
    while curA!=curB:

            curA=headB if not curA else curA.next
            
            curB=headA if not curB else curB.next
        
            
        return curA
    

    '''
    '''

    Definition for singly-linked list.

    class ListNode(object):

    def init(self, x):

    self.val = x

    self.next = None

    import gc
    class Solution(object):
    def getIntersectionNode(self, headA, headB):
    """
    :type head1, head1: ListNode
    :rtype: ListNode
    """
    if not headA or not headB:
    return None
    la=0
    lb=0
    curA=headA
    curB=headB
    while curA and curA.next:
    curA=curA.next
    la+=1
    while curB and curB.next:
    curB=curB.next
    lb+=1
    la+=1
    lb+=1
    if curA!=curB:
    return None
    gc.collect()
    curA=headA
    curB=headB
    if la>lb:
    d=la-lb
    while d:
    curA=curA.next
    d-=1
    else:
    d=lb-la
    while d:
    curB=curB.next
    d-=1
    while curA:
    if curA.val==curB.val:
    return curA
    else:
    curA=curA.next
    curB=curB.next
    return None

    '''


Log in to reply
 

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