> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Merge In Between Linked Lists Python Solution

> Tested Python solution for LeetCode 1669 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 1669, [Medium](/catalog/medium). Topics: [Linked List](/catalog/topics/linked-list). [View on LeetCode](https://leetcode.com/problems/merge-in-between-linked-lists/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1669   # by problem number
lcpy gen -s merge_in_between_linked_lists   # by problem name
```

## Problem

You are given two linked lists: `list1` and `list2` of sizes `n` and `m` respectively.

Remove `list1`'s nodes from the `a<sup>th</sup>` node to the `b<sup>th</sup>` node, and put `list2` in their place.

The blue edges and nodes in the following figure indicate the result:

![Example 1](https://assets.leetcode.com/uploads/2020/11/05/fig1.png)

Build the result list and return its head.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2024/03/01/ll.png)

```
Input: list1 = [10,1,13,6,9,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
Output: [10,1,13,1000000,1000001,1000002,5]
Explanation: We remove the nodes 3 and 4 and put the entire list2 in their place.
```

![Example 2](https://assets.leetcode.com/uploads/2020/11/05/merge_linked_list_ex2.png)

```
Input: list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004]
Output: [0,1,1000000,1000001,1000002,1000003,1000004,6]
```

### Constraints

* `3 <= list1.length <= 10^4`
* `1 <= a <= b < list1.length - 1`
* `1 <= list2.length <= 10^4`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/merge_in_between_linked_lists/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/merge_in_between_linked_lists/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import ListNode


class Solution:
    # Time: O(n + m)
    # Space: O(1)
    def merge_in_between(
        self, list1: ListNode[int] | None, a: int, b: int, list2: ListNode[int] | None
    ) -> ListNode[int] | None:
        if list1 is None:
            return list2
        prev = list1
        for _ in range(a - 1):
            assert prev.next is not None
            prev = prev.next
        tail2 = list2
        while tail2 is not None and tail2.next is not None:
            tail2 = tail2.next
        after = prev
        for _ in range(b - a + 2):
            assert after.next is not None
            after = after.next
        prev.next = list2
        if tail2 is not None:
            tail2.next = after
        return list1
```

## Complexity

| Time | Space |
| - | - |
| O(n + m) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.