🔥One Pass + Start - End Pointer | Clean Code | C++ |
Code\n\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\
# 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        ListNode* start = list1;\n        for (int i = 0; i < a - 1; i++)\n        {\n            start = start->next;\n        }\n\n        ListNode* end = start->next;\n        for (int i = 0; i < b - a + 1; i++)\n        {\n            end = end->next;\n        }\n\n        start->next = list2;\n        while (list2->next)\n        {\n            list2 = list2->next;\n        }\n\n        list2->next = end;\n\n        return list1;\n    }\n};\n```
['C++']