🔥✅✅ Beat 99.56% | ✅ Full Explanation ✅✅🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nFirst,We have to store the refernces to the (a-1)th node and (b+1)th node of the list1 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirst,We have to store the refernces to the (a-1)th node and (b+1)th node of the list1 so that we can use them later for connections to the other list. We have to connect the (a-1)th node of the list1 to the head of the list2. Then go the end of the list2 and connect the end of the list2 to the (b+1)th node of the list1. After that return the updated `list1`.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- First, make temporary pointer references to the list1 and list2, so that we can use them later for connections to the list2.\n```\nListNode* list1_Ptr1 = list1;\nListNode* list1_Ptr2 = list1; \nListNode* list2_Ptr1 = list2; // temporary pointer references\n```\n- Initialise `int i = 0` for checking the current node index.\n- Now, First we have to connect the (a-1)th node\'s next to the head of the list2. So , We have to go to the (a-1)th Node of the list1 and we are storing the (a-1)th node reference in the `list1_Ptr` Pointer.\n```\n for(i; i < a-1; i++) { \n    list1_Ptr1 = list1_Ptr1 -> next; // going to the (a-1)th node of the list1. -\n}\n```\n- Now, we have to connect the end of the list2 to the (b+1)th node of the list1, so first we have to go to the (b+1)th node of the list1.\n\n```\nfor(i = 0; i < b+1; i++) {\n    list1_Ptr2 = list1_Ptr2 -> next; // going to the (b+1)th node of the list1. -\n}\n```\n- Now, connect the (a+1)th node of the list1 to the head of the list2.\n```\nlist1_Ptr1 -> next = list2_Ptr1; // connecting the (a-1)th node of the list1 to the head of the list2.\n```\n- Now , go to the End of the list2, so that we can connect its end to the (b+1)th node of the list1.\n```\n    while(list2_Ptr1 -> next != NULL) {\n        list2_Ptr1 = list2_Ptr1 -> next; // // going to the end of the list2.\n        }\n```\n- Connect the end of the list2 to the (b+1)th node of the list1.\n```\nlist2_Ptr1 -> next = list1_Ptr2; // // connecting the end of the list2 to the (b+1)th node of the list1.\n```\n- Now , return the updated list1.\n```\nreturn list1; // returning the updated linked list list1.\n```\n\n\n\n# Complexity\n- Time complexity: O(m+n) , where m and n are the list1 and list2 sizes respectiely.\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```\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        ListNode* list1_Ptr1 = list1; // for connecting the part of this list1 just from (a-1)th node to 1st node of the list2. \n        ListNode* list1_Ptr2 = list1; // first reach the (b+1)th node of the list1 and then connect the end of the list2 to this (b+1)th node of the list1.\n        ListNode* list2_Ptr1 = list2; // firstly, for connecting the (a-1)th node of the list1 to the head of the list2 and\n                                      // secondly, for going to the end of the list2 and then connect its end to the (b+1)th node of the list1.\n\n        int i = 0; // for checking the current node.\n\n        for(i; i < a-1; i++) { \n            list1_Ptr1 = list1_Ptr1 -> next; // going to the (a-1)th node of the list1.\n        }\n\n        for(i = 0; i < b+1; i++) {\n            list1_Ptr2 = list1_Ptr2 -> next; // going to the (b+1)th node of the list1.\n        }\n\n        list1_Ptr1 -> next = list2_Ptr1; // connecting the (a-1)th node of the list1 to the head of the list2.\n\n        while(list2_Ptr1 -> next != NULL) {\n            list2_Ptr1 = list2_Ptr1 -> next; // going to the end of the list2.\n        }\n\n        list2_Ptr1 -> next = list1_Ptr2; // connecting the end of the list2 to the (b+1)th node of the list1.\n\n        return list1; // returning the updated linked list list1.\n    }\n};\n```\n![upvote.jpeg](https://assets.leetcode.com/users/images/ce196c66-2e6f-4dec-8e30-c25c2f1f7fd3_1706881698.9688694.jpeg)\n![upvote.jpeg](https://assets.leetcode.com/users/images/ce196c66-2e6f-4dec-8e30-c25c2f1f7fd3_1706881698.9688694.jpeg)\n\n\n[Instagram](https://www.instagram.com/iamsidharthaa__/)\n[LinkedIn](https://www.linkedin.com/in/sidharth-jain-36b542133)\n**If you find this solution helpful, consider giving it an upvote! Your support is appreciated. Keep coding, stay healthy, and may your programming endeavors be both rewarding and enjoyable!**
['Linked List', 'C++']