360 ms python solution


  • 0
    S
    class Solution(object):
        def getIntersectionNode(self, headA, headB):
            counterA, counterB = 0, 0
            cur = headA 
            while cur is not None:
                counterA += 1
                cur = cur.next
            cur = headB
            while cur is not None:
                counterB += 1
                cur = cur.next
            indexA, indexB = headA, headB
            if counterA >= counterB:
                for _ in xrange(counterA-counterB):
                    indexA = indexA.next
            else:
                for _ in xrange(counterB - counterA):
                    indexB = indexB.next
            while indexA != indexB:
                indexA = indexA.next
                indexB = indexB.next
            if indexA == indexB:
                return indexA
            else:
                return null

Log in to reply
 

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