Merge In Between Linked Lists
July 27, 2020
Merge In Between Linked Lists
class Solution {
public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
ListNode temp = list1;
ListNode list2LastNode = list2;
while(list2LastNode.next != null){
list2LastNode = list2LastNode.next;
}
int index = 0;
ListNode removeStart = temp;
ListNode removeEnd = temp;
while(temp != null){
if(index == a-1){
removeStart = temp;
}
if(index == b){
removeEnd = temp.next;
break;
}
temp = temp.next;
index++;
}
removeStart.next = list2;
list2LastNode.next = removeEnd;
return list1;
}
}