Merge In Between
Description
Consider two linked lists, list1 and list2, and two integers, i and j. Replace the nodes from the ith to the jth position in list1 with all the nodes from list2.
- The portion of
list1from the 1st to(i-1)th nodes remains unchanged. - All nodes of
list2are inserted after the(i-1)th node. - The portion of
list1starting from the(j+1)th node is appended afterlist2.
Example
i = 2
j = 3
list1 = 1 -> 2 -> 3 -> 4 -> 5
list2 = 6 -> 7 -> 8 -> 9
This problem is a linked-list pointer manipulation task. The key idea is to locate the node just before position i and the node just after position j in list1, then splice list2 into that gap and reconnect the tail of list2 to the remaining suffix of list1. The implementation is straightforward once the boundary pointers are handled carefully.