forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1669.java
36 lines (34 loc) · 1.01 KB
/
_1669.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
public class _1669 {
public static class Solution1 {
public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
ListNode pre = new ListNode(-1);
ListNode list1Temp = list1;
pre.next = list1Temp;
b -= a;
while (a > 1) {
list1Temp = list1Temp.next;
a--;
}
ListNode tail = list1Temp.next;
list1Temp.next = list2;
while (b > 0) {
tail = tail.next;
b--;
}
int length = 0;
ListNode temp2 = list2;
while (temp2 != null) {
temp2 = temp2.next;
length++;
}
while (length > 0) {
list1Temp = list1Temp.next;
length--;
}
list1Temp.next = tail.next;
return pre.next;
}
}
}