✔️100% Beats ✔️Python ✔️C++ ✔️Java ✔️Js ✔️C# ✔️Go
\n\n\n# Approach\nThis solution creates a dummy node to handle cases where the removal starts from the head of list1. Then, it traverses to the (a-1)th node to 
![Capturejava.PNG](https://assets.leetcode.com/users/images/b0f79a47-cab0-43d7-9b3b-199138828325_1710896871.103621.png)\n\n\n# Approach\nThis solution creates a dummy node to handle cases where the removal starts from the head of **list1**. Then, it traverses to the **(a-1)th** node to find the node before the range to be removed. After that, it traverses to the **bth** node to find the node after the range to be removed. It then connects the **(a-1)th** node to the head of **list2**, and traverses to the end of **list2** to connect it with the node after the range to be removed. Finally, it returns the modified **list1**.\n\n# Complexity\n- Time complexity: O(n + m)\n\n- Space complexity: O(1)\n\n# Code\n```python []\nclass Solution:\n    def mergeInBetween(self, list1, a, b, list2):\n        # Create a dummy node to handle cases where the removal starts from the head of list1\n        dummy = ListNode(0)\n        dummy.next = list1\n        \n        # Traverse to the (a-1)th node\n        current = dummy\n        for i in range(a):\n            current = current.next\n        \n        # Save the reference to the (a-1)th node\n        before_a = current\n        \n        # Traverse to the bth node\n        for i in range(a, b + 1):\n            current = current.next\n        \n        # Save the reference to the (b+1)th node\n        after_b = current.next\n        \n        # Connect the (a-1)th node to list2\n        before_a.next = list2\n        \n        # Traverse to the end of list2\n        while list2.next:\n            list2 = list2.next\n        \n        # Connect the end of list2 to the (b+1)th node\n        list2.next = after_b\n        \n        return dummy.next\n```\n\n```cpp []\nclass Solution {\npublic:\n    ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {\n        // Create a dummy node to handle cases where the removal starts from the head of list1\n        ListNode* dummy = new ListNode(0);\n        dummy->next = list1;\n        \n        // Traverse to the (a-1)th node\n        ListNode* current = dummy;\n        for (int i = 0; i < a; ++i) {\n            current = current->next;\n        }\n        \n        // Save the reference to the (a-1)th node\n        ListNode* beforeA = current;\n        \n        // Traverse to the bth node\n        for (int i = a; i <= b; ++i) {\n            current = current->next;\n        }\n        \n        // Save the reference to the (b+1)th node\n        ListNode* afterB = current->next;\n        \n        // Connect the (a-1)th node to list2\n        beforeA->next = list2;\n        \n        // Traverse to the end of list2\n        while (list2->next) {\n            list2 = list2->next;\n        }\n        \n        // Connect the end of list2 to the (b+1)th node\n        list2->next = afterB;\n        \n        return dummy->next;\n    }\n};\n```\n\n```java []\nclass Solution {\n    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {\n        // Create a dummy node to handle cases where the removal starts from the head of list1\n        ListNode dummy = new ListNode(0);\n        dummy.next = list1;\n        \n        // Traverse to the (a-1)th node\n        ListNode current = dummy;\n        for (int i = 0; i < a; i++) {\n            current = current.next;\n        }\n        \n        // Save the reference to the (a-1)th node\n        ListNode beforeA = current;\n        \n        // Traverse to the bth node\n        for (int i = a; i <= b; i++) {\n            current = current.next;\n        }\n        \n        // Save the reference to the (b+1)th node\n        ListNode afterB = current.next;\n        \n        // Connect the (a-1)th node to list2\n        beforeA.next = list2;\n        \n        // Traverse to the end of list2\n        while (list2.next != null) {\n            list2 = list2.next;\n        }\n        \n        // Connect the end of list2 to the (b+1)th node\n        list2.next = afterB;\n        \n        return dummy.next;\n    }\n}\n```\n```javascript []\nvar mergeInBetween = function(list1, a, b, list2) {\n    // Create a dummy node to handle cases where the removal starts from the head of list1\n    const dummy = new ListNode(0);\n    dummy.next = list1;\n    \n    // Traverse to the (a-1)th node\n    let current = dummy;\n    for (let i = 0; i < a; i++) {\n        current = current.next;\n    }\n    \n    // Save the reference to the (a-1)th node\n    const beforeA = current;\n    \n    // Traverse to the bth node\n    for (let i = a; i <= b; i++) {\n        current = current.next;\n    }\n    \n    // Save the reference to the (b+1)th node\n    const afterB = current.next;\n    \n    // Connect the (a-1)th node to list2\n    beforeA.next = list2;\n    \n    // Traverse to the end of list2\n    while (list2.next) {\n        list2 = list2.next;\n    }\n    \n    // Connect the end of list2 to the (b+1)th node\n    list2.next = afterB;\n    \n    return dummy.next;\n}\n```\n```csharp []\npublic class Solution {\n    public ListNode MergeInBetween(ListNode list1, int a, int b, ListNode list2) {\n        // Create a dummy node to handle cases where the removal starts from the head of list1\n        ListNode dummy = new ListNode(0);\n        dummy.next = list1;\n        \n        // Traverse to the (a-1)th node\n        ListNode current = dummy;\n        for (int i = 0; i < a; i++) {\n            current = current.next;\n        }\n        \n        // Save the reference to the (a-1)th node\n        ListNode beforeA = current;\n        \n        // Traverse to the bth node\n        for (int i = a; i <= b; i++) {\n            current = current.next;\n        }\n        \n        // Save the reference to the (b+1)th node\n        ListNode afterB = current.next;\n        \n        // Connect the (a-1)th node to list2\n        beforeA.next = list2;\n        \n        // Traverse to the end of list2\n        while (list2.next != null) {\n            list2 = list2.next;\n        }\n        \n        // Connect the end of list2 to the (b+1)th node\n        list2.next = afterB;\n        \n        return dummy.next;\n    }\n}\n```\n```go []\nfunc mergeInBetween(list1 *ListNode, a int, b int, list2 *ListNode) *ListNode {\n    // Create a dummy node to handle cases where the removal starts from the head of list1\n    dummy := &ListNode{Val: 0, Next: list1}\n    \n    // Traverse to the (a-1)th node\n    current := dummy\n    for i := 0; i < a; i++ {\n        current = current.Next\n    }\n    \n    // Save the reference to the (a-1)th node\n    beforeA := current\n    \n    // Traverse to the bth node\n    for i := a; i <= b; i++ {\n        current = current.Next\n    }\n    \n    // Save the reference to the (b+1)th node\n    afterB := current.Next\n    \n    // Connect the (a-1)th node to list2\n    beforeA.Next = list2\n    \n    // Traverse to the end of list2\n    for list2.Next != nil {\n        list2 = list2.Next\n    }\n\n    // Connect the end of list2 to the (b+1)th node\n    list2.Next = afterB\n\n    return dummy.Next;\n}\n```\n![upvote_new_cc82e8aa-dc98-4f10-af4c-64b1528c0fb6_1680844232.130332.png](https://assets.leetcode.com/users/images/f9049ccb-5e94-4dfc-b9b7-0e4213b97c82_1710896557.3198626.png)\n\n
['C#']