My accepted Python solution: easy to read, hope useful.


  • -8
    Q
    def getIntersectionNode(self, headA, headB):
        if headA == None or headB == None:
            return None
        ptr_A = headA
        ptr_B = headB
        # From the test cases, you can see all list are soreted.
        while ptr_A != None and ptr_B != None:
            if ptr_A.val < ptr_B.val:
                ptr_A = ptr_A.next
            elif ptr_B.val < ptr_A.val:
                ptr_B = ptr_B.next
            else:
                return ptr_A
    
        return None

  • 0
    S

    What if the lists aren't sorted then? The solution should be general enough to not have a specific input that wasn't explicitly specified in the instructions.


  • 0
    M

    good catch songking - I have the exact same question ! All of the solutions seem to assume that the two lists are sorted list with integers . And question doesnt say this at all.


Log in to reply
 

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