Skip to content

Commit 0ee163b

Browse files
authored
Add solution and testcase for 1669 (fishercoder1534#157)
* fix: add solution and testcase for 1669 * fix: add solution and testcase for 1669
1 parent ea2de74 commit 0ee163b

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/main/java/com/fishercoder/solutions/_1669.java

+21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
public class _1669 {
66
public static class Solution1 {
77
public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
8+
89
ListNode pre = new ListNode(-1);
910
ListNode list1Temp = list1;
1011
pre.next = list1Temp;
@@ -33,4 +34,24 @@ public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
3334
return pre.next;
3435
}
3536
}
37+
public static class Solution2 {
38+
public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
39+
ListNode endList = list1;
40+
ListNode startList = null;
41+
42+
for (int i = 0; i < b; i++, endList = endList.next) {
43+
if (i == a - 1) {
44+
startList = endList;
45+
}
46+
}
47+
// Connect the startList.next to list2
48+
startList.next = list2;
49+
while (list2.next != null) {
50+
list2 = list2.next;
51+
}
52+
list2.next = endList.next;
53+
endList.next = null;
54+
return list1;
55+
}
56+
}
3657
}

src/test/java/com/fishercoder/_1669Test.java

+17-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
import com.fishercoder.common.classes.ListNode;
44
import com.fishercoder.common.utils.LinkedListUtils;
5-
import com.fishercoder.solutions._1;
6-
import com.fishercoder.solutions._1669;
75
import org.junit.BeforeClass;
6+
import org.junit.Test;import org.junit.BeforeClass;
87
import org.junit.Test;
9-
10-
import static org.junit.Assert.assertArrayEquals;
11-
import static org.junit.Assert.assertEquals;
8+
import com.fishercoder.solutions._1669;
129

1310
public class _1669Test {
1411
private static _1669.Solution1 solution1;
12+
private static _1669.Solution1 solution2;
13+
private static ListNode l1;
14+
private static ListNode l2;
15+
private static int a;
16+
private static int b;
1517
private static ListNode list1;
1618
private static ListNode list2;
1719
private static ListNode expected;
@@ -20,6 +22,7 @@ public class _1669Test {
2022
@BeforeClass
2123
public static void setup() {
2224
solution1 = new _1669.Solution1();
25+
solution2 = new _1669.Solution2();
2326
}
2427

2528
@Test
@@ -31,5 +34,12 @@ public void test1() {
3134
LinkedListUtils.printList(actual);
3235
assertEquals(expected, actual);
3336
}
34-
35-
}
37+
@Test
38+
public void test2() {
39+
l1 = ListNode.createSinglyLinkedList(Arrays.asList(0, 1, 2, 3, 4, 5));
40+
l2 = ListNode.createSinglyLinkedList(Arrays.asList(1000000,1000001,1000002));
41+
a = 3;
42+
b = 4;
43+
assertEquals(ListNode.createSinglyLinkedList(Arrays.asList(0,1,2,1000000,1000001,1000002,5)), solution2.mergeInBetween(l1, a, b, l2));
44+
}
45+
}

0 commit comments

Comments
 (0)