You are given two linked lists: list1 and list2 of sizes n and m respectively.

Remove list1's nodes from the ath node to the bth node, and put list2 in their place.

The blue edges and nodes in the following figure indicate the result:

Build the result list and return its head.

&nbsp;
Example 1:

Input: list1 = [0,1,2,3,4,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
Output: [0,1,2,1000000,1000001,1000002,5]
Explanation: We remove the nodes 3 and 4 and put the entire list2 in their place. The blue edges and nodes in the above figure indicate the result.


Example 2:

Input: list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004]
Output: [0,1,1000000,1000001,1000002,1000003,1000004,6]
Explanation: The blue edges and nodes in the above figure indicate the result.


&nbsp;
Constraints:


	3 &lt;= list1.length &lt;= 104
	1 &lt;= a &lt;= b &lt; list1.length - 1
	1 &lt;= list2.length &lt;= 104


class Solution:
    def mergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode:
        head = list1
        for _ in range(a-1):
            head = head.next
        cur = head.next
        for _ in range(b-a):
            cur = cur.next
        head.next = list2
        while head.next:
            head = head.next
        if cur.next:
            head.next = cur.next
        return list1
class Solution {
    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        ListNode left = list1;
        for (int i = 1; i < a; i++)
            left = left.next;
        
        ListNode middle = left;
        for (int i = a; i <= b; i++)
            middle = middle.next;
        
		left.next = list2;
        while (list2.next != null)
            list2 = list2.next;
        
        list2.next = middle.next;
        return list1;
    }
}
class Solution {
public:
    ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
        
        int jump1 = 1;
        ListNode *temp1 = list1;
        while (jump1 < a){
            temp1 = temp1->next;
            jump1++;
        }                                //Gets the pointer to a
        
		int jump2 = 1;
        ListNode *temp2 = list1;
        while(jump2 <= b){
            temp2 = temp2->next;
            jump2++;
        }                                //Gets the pointer to b
        
		ListNode *temp3=list2;
        while(temp3->next != NULL){
            temp3=temp3->next;
        }                               //Gets the pointer to the tail of list2
        
		
		temp1->next=list2;              //set the next pointer of a to the head of list2
        
		temp3->next = temp2->next;      //set next of tail of list2 to the pointer to b
        
		return list1;                   //return the original list i.e. list1
        
    }
};
var mergeInBetween = function(list1, a, b, list2) {
    let start = list1;
    let end = list1;

    for (let i = 0; i <= b && start != null && end != null; i++) {
        if (i < a - 1) start = start.next;
        if (i <= b) end = end.next;
    }

    let tail = list2;

    while (tail.next != null) {
        tail = tail.next;
    }

    start.next = list2;
    tail.next = end;

    return list1;
};
Merge In Between Linked Lists