Beats 100% of users with C++ || Step by Step Explain || Fast Solution ||
Abhiraj Pratap Singh\n\n---\n\n# UPVOTE IT....\n\n\n\n---\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n- The mergeInBetween fun
# Abhiraj Pratap Singh\n\n---\n\n# UPVOTE IT....\n\n\n\n---\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The mergeInBetween function appears to merge two singly-linked lists list1 and list2 by removing a portion of nodes from list1 between the indices a and b (inclusive) and inserting the nodes of list2 in their place.\n\n\n---\n\n![Screenshot (47).png](https://assets.leetcode.com/users/images/de11151a-c6cd-4ac8-b76a-2b4a3ac9a262_1705078223.188123.png)\n\n\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n1. Iterate through the nodes of list1 until reaching the node at index a-1. Set a pointer (temp1) to this node.\n2. Iterate through the nodes of list1 until reaching the node at index b+1. Set a pointer (temp2) to this node.\n3. Iterate through the nodes of list2 until reaching the last node. Set a pointer (ptr2) to this node.\n4. Update the next pointer of temp1 to point to the head of list2.\n5. Update the next pointer of the last node of list2 (pointed by ptr2) to point to temp2.\n6. Return the modified list1.\n\n---\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- **Time complexity: O(n+m),** where n is the length of list1 and m is the length of list2. The function iterates through both lists once.\n\n\n---\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n- **Space complexity: O(1),** The space required is constant as the function uses a constant number of pointers regardless of the size of the input lists.\n\n\n---\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    {\n        int Break_point=2;\n        int node_no=0;\n        ListNode *ptr1=list1;\n        ListNode *ptr2=list2;\n        ListNode *temp1=NULL;\n        ListNode *temp2=NULL;\n        while( Break_point != 0)\n        {\n            if(node_no==a-1)\n            {\n                Break_point--;\n                temp1=ptr1;\n            }\n            if( node_no== b+1)\n            {\n                Break_point--;\n                temp2=ptr1;\n            }\n            node_no++;\n            ptr1=ptr1->next;\n        }    \n        while(ptr2!=NULL && ptr2->next!=NULL)\n            ptr2=ptr2->next;\n        temp1->next=list2;\n        ptr2->next=temp2;\n        return list1;\n    }\n};\n```\n\n---\n\n# if you like the solution please UPVOTE it.....\n\n---\n![discord-discordgifemoji.gif](https://assets.leetcode.com/users/images/507565e9-e163-4f19-8c87-0f7db423bfe2_1705078519.837964.gif)\n\n---\n\n![fucking-die-redditor-stupid-cuck-funny-cat.gif](https://assets.leetcode.com/users/images/8bb8ebd4-b866-4ece-a15b-92904b29ed8d_1705078523.4935737.gif)\n\n---\n---
['Linked List', 'C++']