<p>You are given two linked lists: <code>list1</code> and <code>list2</code> of sizes <code>n</code> and <code>m</code> respectively.</p>

<p>Remove <code>list1</code>&#39;s nodes from the <code>a<sup>th</sup></code> node to the <code>b<sup>th</sup></code> node, and put <code>list2</code> in their place.</p>

<p>The blue edges and nodes in the following figure indicate the result:</p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/fig1.png" style="height: 130px; width: 504px;" />
<p><em>Build the result list and return its head.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2024/03/01/ll.png" style="width: 609px; height: 210px;" />
<pre>
<strong>Input:</strong> list1 = [10,1,13,6,9,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
<strong>Output:</strong> [10,1,13,1000000,1000001,1000002,5]
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/merge_linked_list_ex2.png" style="width: 463px; height: 140px;" />
<pre>
<strong>Input:</strong> list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004]
<strong>Output:</strong> [0,1,1000000,1000001,1000002,1000003,1000004,6]
<strong>Explanation:</strong> The blue edges and nodes in the above figure indicate the result.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= list1.length &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= a &lt;= b &lt; list1.length - 1</code></li>
	<li><code>1 &lt;= list2.length &lt;= 10<sup>4</sup></code></li>
</ul>

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
        ListNode* itr1 = list1, *itr2 = list2;
        int index = 0;

        // Moving itr1 for (a - 1)th node.
        while(index < a - 1) {
            itr1 = itr1->next;
            index++;
        }
        // cout << index << endl;
        // Storing address of (b + 1) th node
        ListNode* itr = itr1;

        while(index <= b) {
            itr = itr->next;
            index++;
        }
        // cout << index << endl;

        // Changing next of (a - 1) th node.
        itr1->next = itr2;

        // Setting itr2 to the last node of list2.
        while(itr2->next)
            itr2 = itr2->next;

        // Setting the next of last node to the (b + 1) th node.
        itr2->next = itr;
        
        return list1;

    }
};

int init = [] {
    ofstream out("user.out");
    cout.rdbuf(out.rdbuf());    
    int a, b;    
    string s1, s2;
    while( cin >> s1 >> a >> b >> s2) {
        b++;              
        int a1{};
        int b1{};
        for (int i{1}, c{}; ; ++i) {
            if (s1[i] == ',') {
                ++c;
                if (c == a) a1 = i;
                else if (c == b) {
                    b1 = i;
                    break;
                }
            }
        }
        std::string_view l0{s1.begin(), s1.begin() + a1 + 1};
        std::string_view l1{s2.begin() + 1, s2.end() - 1};
        std::string_view l2{s1.begin() + b1, s1.end()};
        cout << l0 << l1 << l2 << '\n';
        
    }
    exit(0);
    return 0;
}();
8100