Problem statement

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

Solution

Equal to Leetcode 0021. Merge Two Sorted Lists, but with some more edge cases.

Complexity

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

Code

class Solution:
    def solve(self, l0, l1):
        dummy = LLNode(-1)
        res = dummy
        while l0 or l1:
            v0 = l0.val if l0 else float("inf")
            v1 = l1.val if l1 else float("inf")
            if v0 <= v1: l0 = l0.next
            if v0 >= v1: l1 = l1.next
            res.next = LLNode(min(v0, v1))
            res = res.next

        return dummy.next