Python solution in O(n) time and use only O(1) memory

  • 0
    class Solution(object):
        def getIntersectionNode(self, headA, headB):
            :type head1, head1: ListNode
            :rtype: ListNode
            if not headA or not headB:
                return None
            t2 = headB
                t2 =
   = headB
            dummy = ListNode(-1)
   = headA
            fast = dummy
            slow = dummy
            flag = False
            while fast and
                fast =
                slow =
                if fast == slow:
                    flag = True
            if not flag:
                dummy = None
       = None
                return None
            tmp = dummy
            while tmp != slow:
                tmp =
                slow =
            dummy = None
   = None
            return tmp

Log in to reply

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