✅ BEST C++ SOLUTION ☑️
PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT\n# CONNECT WITH ME\n### https://www.linkedin.com/in/pratay-nandy-9ba57b229/\n#### https://www.instagram.com/pratay_nand
# **PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT**\n# **CONNECT WITH ME**\n### **[https://www.linkedin.com/in/pratay-nandy-9ba57b229/]()**\n#### **[https://www.instagram.com/_pratay_nandy_/]()**\n\n# Approach\n\n\n### Let\'s break down the functionality of this function:\n\n1. **Initialization**:\n   - It initializes three pointers `p`, `q`, and `r` to the head nodes of `list1`, `list1`, and `list2`, respectively.\n\n2. **Finding the Tail of `list2`**:\n   - It iterates through `list2` to find its tail node `r`. This is done to determine where to append the nodes of `list2` to `list1`.\n\n3. **Moving `p` to the Node Before Index `a`**:\n   - It decrements the value of `a` by 1 (assuming the indices are 0-based).\n   - It then traverses `list1` by moving the pointer `p` to the node just before the node at index `a`.\n\n4. **Moving `q` to the Node After Index `b`**:\n   - It increments the value of `b` by 1.\n   - It then traverses `list1` by moving the pointer `q` to the node just after the node at index `b`.\n\n5. **Merging `list2` into `list1`**:\n   - It updates the `next` pointer of the node pointed to by `p` to point to the head of `list2`, effectively inserting `list2` into `list1`.\n   - It updates the `next` pointer of the tail node of `list2` (pointed to by `r`) to point to the node pointed to by `q`, connecting the remainder of `list1` after the removed portion.\n   \n6. **Returning the Head of the Merged List**:\n   - It returns the head node of `list1`, which now contains the merged list.\n\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:**0(N)**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:**0(1)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n![upvote2.png](https://assets.leetcode.com/users/images/6198d926-527c-42f7-a663-aaf8d62ed44c_1704980133.0159707.png)\n\n**Bold**\n# Code\n```\nclass Solution {\npublic:\n    ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {\n        ListNode* p = list1;\n        ListNode* q = list1;\n        ListNode* r = list2;\n        while(r->next!=NULL)\n        {\n            r = r->next;\n        }\n        a = a-1; \n        while(a > 0)\n        {\n            p = p->next;\n            a--;\n        }\n        b+=1;\n        while(b>0)\n        {\n            q = q->next;\n            b--;\n        }\n        p->next = list2; \n        r->next = q; \n        return list1;\n    }\n};\n```
['Linked List', 'C++']