Merge In Between Linked Lists || Optimal Solution || Java || Easy to understand || 100% beats
Merge In Between Linked Lists\n\n## Intuition\nTo solve this problem, we need to remove a range of nodes from the list1 and replace them with the nodes from lis
# Merge In Between Linked Lists\n\n## Intuition\nTo solve this problem, we need to remove a range of nodes from the `list1` and replace them with the nodes from `list2`. This involves finding the nodes at positions `a` and `b` in `list1`, connecting the node before `a` to the head of `list2`, and connecting the last node of `list2` to the node after `b`.\n\n## Approach\n1. Traverse through `list1` to find the ath node and connect the node before `a` to the head of `list2`.\n2. Traverse through `list2` to find the last node.\n3. Traverse through `list1` to find the (b+1)th node and connect the last node of `list2` to it.\n\n## Complexity\n- Time complexity: O(n + m)\n  - We traverse through `list1` once to find the ath and (b+1)th nodes, which takes O(n) time.\n  - We traverse through `list2` once to find the last node.\n- Space complexity: O(1)\n  - We use only a constant amount of extra space.\n\n![image.png](https://assets.leetcode.com/users/images/6efc2724-4efa-455b-8a99-c7164e03d3b3_1710917054.3201027.png)\n\n## Code\n\n```java []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {\n        // Find the ath node in list1\n        int count = 0;\n        ListNode current = list1;\n        while (count < a - 1) {\n            current = current.next;\n            count++;\n        }\n\n        // Connect the ath node to the head of list2\n        ListNode aNode = current;\n        while (count < b + 1) {\n            current = current.next;\n            count++;\n        }\n\n        // Connect the last node of list2 to the node after b\n        ListNode lastNodeOfList2 = list2;\n        while (lastNodeOfList2.next != null) {\n            lastNodeOfList2 = lastNodeOfList2.next;\n        }\n        lastNodeOfList2.next = current;\n\n        // Connect the node before a to the head of list2\n        aNode.next = list2;\n\n        return list1;\n    }\n}\n\n```\n```python []\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        # Find the ath node in list1\n        count = 0\n        current = list1\n        while count < a - 1:\n            current = current.next\n            count += 1\n\n        # Connect the ath node to the head of list2\n        a_node = current\n        while count < b + 1:\n            current = current.next\n            count += 1\n\n        # Connect the last node of list2 to the node after b\n        last_node_of_list2 = list2\n        while last_node_of_list2.next is not None:\n            last_node_of_list2 = last_node_of_list2.next\n        last_node_of_list2.next = current\n\n        # Connect the node before a to the head of list2\n        a_node.next = list2\n\n        return list1\n\n```\n```C++ []\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n    ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {\n        // Find the ath node in list1\n        int count = 0;\n        ListNode* current = list1;\n        while (count < a - 1) {\n            current = current->next;\n            count++;\n        }\n\n        // Connect the ath node to the head of list2\n        ListNode* aNode = current;\n        while (count < b + 1) {\n            current = current->next;\n            count++;\n        }\n\n        // Connect the last node of list2 to the node after b\n        ListNode* lastNodeOfList2 = list2;\n        while (lastNodeOfList2->next != nullptr) {\n            lastNodeOfList2 = lastNodeOfList2->next;\n        }\n        lastNodeOfList2->next = current;\n\n        // Connect the node before a to the head of list2\n        aNode->next = list2;\n\n        return list1;\n    }\n};\n\n```\n```C []\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     struct ListNode *next;\n * };\n */\n\n\nstruct ListNode* mergeInBetween(struct ListNode* list1, int a, int b, struct ListNode* list2){\n    struct ListNode* current = list1;\n    int count = 0;\n\n    // Find the ath node in list1\n    while (count < a - 1) {\n        current = current->next;\n        count++;\n    }\n\n    // Connect the ath node to the head of list2\n    struct ListNode* aNode = current;\n    while (count < b + 1) {\n        current = current->next;\n        count++;\n    }\n\n    // Connect the last node of list2 to the node after b\n    struct ListNode* lastNodeOfList2 = list2;\n    while (lastNodeOfList2->next != NULL) {\n        lastNodeOfList2 = lastNodeOfList2->next;\n    }\n    lastNodeOfList2->next = current;\n\n    // Connect the node before a to the head of list2\n    aNode->next = list2;\n\n    return list1;\n}\n\n```\n```JavaScript []\n/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n *     this.val = (val===undefined ? 0 : val)\n *     this.next = (next===undefined ? null : next)\n * }\n */\n\nvar mergeInBetween = function(list1, a, b, list2) {\n    let current = list1;\n    let count = 0;\n\n    // Find the ath node in list1\n    while (count < a - 1) {\n        current = current.next;\n        count++;\n    }\n\n    // Connect the ath node to the head of list2\n    let aNode = current;\n    while (count < b + 1) {\n        current = current.next;\n        count++;\n    }\n\n    // Connect the last node of list2 to the node after b\n    let lastNodeOfList2 = list2;\n    while (lastNodeOfList2.next !== null) {\n        lastNodeOfList2 = lastNodeOfList2.next;\n    }\n    lastNodeOfList2.next = current;\n\n    // Connect the node before a to the head of list2\n    aNode.next = list2;\n\n    return list1;\n};\n\n```\n\n
['Linked List', 'C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']