Why it doesn't work in python

  • 0

    I think it's completely correct, but encountered runtime error in python.

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    class Solution(object):
        def getIntersectionNode(self, headA, headB):
            :type head1, head1: ListNode
            :rtype: ListNode
            def getl(head):
                l = 0
                while head:
                    l += 1
                    head = head.next
                return l
            def helper(l1, l2, diff):
                if not l1 or not l2: return None
                if diff == 0:
                    if l1.val == l2.val:
                        return l1
                        return helper(l1.next, l2.next, 0)
                    return helper(l1.next, l2, diff-1)        
            if not headA or not headB:
                return None
            n1, n2 = getl(headA), getl(headB)
            if n1 > n2:
                return helper(headA, headB, n1-n2)
                return helper(headB, headA, n2-n1)

Log in to reply

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