Merge In Between Linked Lists LeetCode solution in C++
1class Solution {2public:3ListNode *mergeInBetween(ListNode *list1, int a, int b, ListNode *list2) {4int jump1 = 1;5ListNode *temp1 = list1;6while (jump1 < a) {7temp1 = temp1->next;8jump1++;9} // Gets the pointer to a1011int jump2 = 1;12ListNode *temp2 = list1;13while (jump2 <= b) {14temp2 = temp2->next;15jump2++;16} // Gets the pointer to b1718ListNode *temp3 = list2;19while (temp3->next != NULL) {20temp3 = temp3->next;21} // Gets the pointer to the tail of list22223temp1->next = list2; // set the next pointer of a to the head of list22425temp3->next = temp2->next; // set next of tail of list2 to the pointer to b2627return list1; // return the original list i.e. list128}29};