Problem statement

https://binarysearch.com/problems/Linked-List-Intersection/

Solution

Use two pointers approach to find intersection

Complexity

It is O(n + m) for time and O(min(n, m)) for space.

Code

class Solution:
    def solve(self, l0, l1):
        dummy = LLNode(-1)
        res = dummy
        while l0 and l1:
            if l0.val < l1.val:
                l0 = l0.next
            elif l0.val > l1.val:
                l1 = l1.next
            else:
                res.next = LLNode(l0.val)
                res = res.next
                l0 = l0.next
                l1 = l1.next

        return dummy.next