Problem statement

https://leetcode.com/problems/merge-in-between-linked-lists/

Solution

Just do what is asked here: find place and then insert list.

Complexity

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

Code

class Solution:
    def mergeInBetween(self, a, lo, hi, b):
        start, temp, tempb = a, a, b

        for ind in range(hi + 1):
            if ind == lo - 1: start = temp
            temp = temp.next

        while tempb.next: tempb = tempb.next

        start.next, tempb.next = b, temp
        return a if lo else a.next