What's the difference between these codes?


  • 0
    B

    I've tested two ways for several times and the first one is always 100ms to 200ms quicker than second one, I really don't understand why.
    First:

    class Solution(object):
        def getIntersectionNode(self, headA, headB):
            """
            :type head1, head1: ListNode
            :rtype: ListNode
            """
            if headA is None or headB is None:
                return None
            
            h1 = headA
            h2 = headB
            while h1 is not h2:
                h1 = h1.next if h1 else headB
                h2 = h2.next if h2 else headA
            return h1
    

    Second:

    class Solution(object):
        def getIntersectionNode(self, headA, headB):
            """
            :type head1, head1: ListNode
            :rtype: ListNode
            """
            if not headA or not headB:
                return None
            A=headA
            B=headB
            while A is not B:
                A=A.next if A else headB
                B=B.next if B else headA
            return A
    '''

Log in to reply
 

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