🏆 Easy to Understand Python Solution with 99% Running Time 🚀
Intuition\n Describe your first thoughts on how to solve this problem. \nTo merge list2 into list1 between indices a and b, we need to locate the nodes at posit
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo merge list2 into list1 between indices a and b, we need to locate the nodes at positions a-1 and b+1 in list1, and link them correctly to list2.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTraverse list1 to find the nodes at positions a-1 and b+1.\nTraverse list2 to find the last node.\nLink the node at position a-1 in list1 to the head of list2.\nLink the last node of list2 to the node at position b+1 in list1.\n# Complexity\n- Time complexity:O(n+m) where \nn is the length of list1 and \nm is the length of list2.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: \nO(1) since we are only using a few extra pointers.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# Definition for singly-linked list.\n# class ListNode(object):\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution(object):\n    def mergeInBetween(self, list1, a, b, list2):\n        """\n        :type list1: ListNode\n        :type a: int\n        :type b: int\n        :type list2: ListNode\n        :rtype: ListNode\n        """\n        count = 0\n        prev, curr = ListNode(None), list1\n        ret = list1\n        temp2, last2 = list2, list2\n\n        while last2 and last2.next:\n            last2 = last2.next\n\n        found = None\n        b += 1\n        while curr:\n            count += 1\n            if count == a:\n                prev = curr\n            elif count == b + 1:\n                found = curr\n                prev.next = temp2\n                last2.next = curr\n            curr = curr.next\n        return ret\n```\n![guy_in_toilet.jpg](https://assets.leetcode.com/users/images/536131e8-8045-4af0-85d7-17704d9fe717_1722175579.8204393.jpeg)\n
['Linked List', 'Python', 'Python3']