Simple Traversal Solution || C++ || JAVA
Intuition\nThe problem requires merging a sublist of list1 defined by the indices a and b, with another linked list list2. The straightforward approach involves
# Intuition\nThe problem requires merging a sublist of list1 defined by the indices a and b, with another linked list list2. The straightforward approach involves traversing list1 to reach the node just before index a and the node at index b. Then, we point the next of the node at index a - 1 to list2, traverse to the end of list2, and connect it with the node at index b + 1.\n\n# Approach\nTraverse list1 to reach the node just before index a.\nTraverse list1 again to reach the node at index b.\nConnect the next of the node at index a - 1 to list2.\nTraverse list2 to reach its end.\nConnect the last node of list2 to the node at index b + 1.\nReturn list1.\n# Complexity\nTime complexity: $$O(n)$$, where n is the length of list1.\nSpace complexity: $$O(1)$$, as the space used is constant irrespective of the input size.\n\n# Code\n```cpp []\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 *temp1 = list1;\n        ListNode *temp2 = list1;\n        \n        // Traverse to the node just before index \'a\'\n        for (int i = 0; i < a - 1; i++) {\n            temp1 = temp1->next;\n        }\n        \n        // Traverse to the node at index \'b\'\n        for (int i = 0; i < b; i++) {\n            temp2 = temp2->next;\n        }\n        \n        // Connect the sublist of \'list1\' with \'list2\'\n        temp1->next = list2;\n        \n        // Traverse to the end of \'list2\'\n        while (list2->next != nullptr) {\n            list2 = list2->next;\n        }\n        \n        // Connect the last node of \'list2\' with the node after index \'b\'\n        list2->next = temp2->next;\n        \n        return list1;\n    }\n};\n\n```\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        ListNode temp1=list1;\n        ListNode temp2=list1;\n        for(int i=0;i<b+1;i++){\n            temp2=temp2.next;\n        }\n        for(int i=0;i<a-1;i++){\n            temp1=temp1.next;\n        }\n        temp1.next=list2;\n        while(temp1.next!=null){\n            temp1=temp1.next;\n        }\n        temp1.next=temp2;\n        System.gc();\n        return list1;\n    }\n}\n```
['C++', 'Java']