Beats 100%🚀➡️Intuitive Solution Full Explained ➡️[Java/C++/C/C#/JavaScript/Python3/Rust]
Intuition\n1. We have to get the node before a and node after b.\n2. We will attach the node before a to the start of list2, and add the the end of the list2 to
# Intuition\n1. We have to get the node before a and node after b.\n2. We will attach the node before a to the start of list2, and add the the end of the list2 to the node after b.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. **Check for `null` lists:** \n    - The method begins by checking if either `list1` or `list2` is null. \n    - If either is `null`, it returns `null` immediately, indicating that there\'s nothing to merge.\n\n2. **Initialize pointers and variables:** \n    - It initializes several pointers and a counter variable.\n    - `curr`, `start`, and end are ListNode pointers initially pointing to `list1`.\n    - `count` is an integer variable initialized to `0`.\n3. **Traverse list1 to find start and end points:** \n    - It traverses `list1` using the `curr` pointer while counting the number of nodes visited `(count)`.\n    - When the `count` equals `a`, it sets `start` to the current node.\n    - When the `count` equals `b + 1`, it sets `end` to the current node.\n4. **Merge list2 between start and end points:** \n    - After finding the appropriate `start` and `end` points in `list1`, it sets the next pointer of start to `list2`, effectively merging `list2` into `list1`.\n    - It then traverses `list2` to find its `end` node `(curr)` and sets the next pointer of `curr` to `end`.\n5. **Return the modified list1:** \n    - Finally, it returns the modified `list1`.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```Java []\nclass Solution {\n    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {\n        if(list1==null || list2==null) {\n            return null;\n        }\n        ListNode curr = list1;\n        ListNode start = list1;\n        ListNode end =list1;\n        int count = 0;\n\n        while(curr!=null) {\n            count++;\n            if(count==a) {\n                start = curr;\n            }\n            curr = curr.next;\n            if(count==b+1) {\n                end = curr;\n            }\n        }\n\n        curr = list2;\n        start.next = list2;\n        while(curr.next!=null) {\n            curr=curr.next;\n        }\n\n        curr.next = end;\n        return list1;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {\n        if (!list1 || !list2) {\n            return nullptr;\n        }\n        ListNode* curr = list1;\n        ListNode* start = list1;\n        ListNode* end = list1;\n        int count = 0;\n\n        while (curr) {\n            count++;\n            if (count == a) {\n                start = curr;\n            }\n            curr = curr->next;\n            if (count == b + 1) {\n                end = curr;\n            }\n        }\n\n        curr = list2;\n        start->next = list2;\n        while (curr->next) {\n            curr = curr->next;\n        }\n\n        curr->next = end;\n        return list1;\n    }\n};\n```\n```Python3 []\nclass Solution:\n    def mergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode:\n        if not list1 or not list2:\n            return None\n        curr = list1\n        start = list1\n        end = list1\n        count = 0\n\n        while curr:\n            count += 1\n            if count == a:\n                start = curr\n            curr = curr.next\n            if count == b + 1:\n                end = curr\n\n        curr = list2\n        start.next = list2\n        while curr.next:\n            curr = curr.next\n\n        curr.next = end\n        return list1\n```\n```Go []\nfunc mergeInBetween(list1 *ListNode, a int, b int, list2 *ListNode) *ListNode {\n    if list1 == nil || list2 == nil {\n        return nil\n    }\n    curr := list1\n    start := list1\n    end := list1\n    count := 0\n\n    for curr != nil {\n        count++\n        if count == a {\n            start = curr\n        }\n        curr = curr.Next\n        if count == b+1 {\n            end = curr\n        }\n    }\n\n    curr = list2\n    start.Next = list2\n    for curr.Next != nil {\n        curr = curr.Next\n    }\n\n    curr.Next = end\n    return list1\n}\n```\n```C# []\npublic class Solution {\n    public ListNode MergeInBetween(ListNode list1, int a, int b, ListNode list2) {\n        if (list1 == null || list2 == null) {\n            return null;\n        }\n        ListNode curr = list1;\n        ListNode start = list1;\n        ListNode end = list1;\n        int count = 0;\n\n        while (curr != null) {\n            count++;\n            if (count == a) {\n                start = curr;\n            }\n            curr = curr.next;\n            if (count == b + 1) {\n                end = curr;\n            }\n        }\n\n        curr = list2;\n        start.next = list2;\n        while (curr.next != null) {\n            curr = curr.next;\n        }\n\n        curr.next = end;\n        return list1;\n    }\n}\n```\n```Rust []\nimpl Solution {\n    pub fn merge_in_between(list1: Option<Box<ListNode>>, a: i32, b: i32, list2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {\n        let mut list1 = list1;\n        let mut list2 = list2;\n\n        if list1.is_none() || list2.is_none() {\n            return None;\n        }\n\n        let mut curr = list1.as_mut().unwrap().as_mut();\n        let mut start = curr;\n        let mut end = curr;\n        let mut count = 0;\n\n        while let Some(node) = curr {\n            count += 1;\n            if count == a {\n                start = node;\n            }\n            curr = node.next.as_mut().map(|n| n.as_mut());\n            if count == b + 1 {\n                end = curr.unwrap();\n            }\n        }\n\n        let mut curr = list2.as_mut().unwrap().as_mut();\n        start.next = list2;\n        while let Some(node) = curr {\n            if let Some(next) = &mut node.next {\n                curr = next.as_mut();\n            } else {\n                break;\n            }\n        }\n\n        curr.unwrap().next = end.next;\n        list1\n    }\n}\n```\n```JavaScript []\n/**\n * Definition for singly-linked list.\n */\nfunction ListNode(val, next) {\n    this.val = (val === undefined ? 0 : val);\n    this.next = (next === undefined ? null : next);\n}\n\n/**\n * @param {ListNode} list1\n * @param {number} a\n * @param {number} b\n * @param {ListNode} list2\n * @return {ListNode}\n */\nvar mergeInBetween = function(list1, a, b, list2) {\n    if (!list1 || !list2) {\n        return null;\n    }\n    let curr = list1;\n    let start = list1;\n    let end = list1;\n    let count = 0;\n\n    while (curr) {\n        count++;\n        if (count === a) {\n            start = curr;\n        }\n        curr = curr.next;\n        if (count === b + 1) {\n            end = curr;\n        }\n    }\n\n    curr = list2;\n    start.next = list2;\n    while (curr.next) {\n        curr = curr.next;\n    }\n\n    curr.next = end;\n    return list1;\n};\n```\n\n---\n\n\n---\n\n![5c63d377-8ef4-4beb-b09d-0edb07e09a41_1702955205.6568592.png](https://assets.leetcode.com/users/images/2547df47-13b1-4e5d-92bc-f9bcf57b4eda_1710928620.4295084.png)\n\n
['C', 'Python', 'C++', 'Java', 'Python3', 'Rust', 'JavaScript', 'C#']