Skip to content

Add solution and testcase for 1669 #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1669.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -33,4 +34,24 @@ public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
return pre.next;
}
}
public static class Solution2 {
public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
ListNode endList = list1;
ListNode startList = null;

for (int i = 0; i < b; i++, endList = endList.next) {
if (i == a - 1) {
startList = endList;
}
}
// Connect the startList.next to list2
startList.next = list2;
while (list2.next != null) {
list2 = list2.next;
}
list2.next = endList.next;
endList.next = null;
return list1;
}
}
}
24 changes: 17 additions & 7 deletions src/test/java/com/fishercoder/_1669Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

import com.fishercoder.common.classes.ListNode;
import com.fishercoder.common.utils.LinkedListUtils;
import com.fishercoder.solutions._1;
import com.fishercoder.solutions._1669;
import org.junit.BeforeClass;
import org.junit.Test;import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import com.fishercoder.solutions._1669;

public class _1669Test {
private static _1669.Solution1 solution1;
private static _1669.Solution1 solution2;
private static ListNode l1;
private static ListNode l2;
private static int a;
private static int b;
private static ListNode list1;
private static ListNode list2;
private static ListNode expected;
Expand All @@ -20,6 +22,7 @@ public class _1669Test {
@BeforeClass
public static void setup() {
solution1 = new _1669.Solution1();
solution2 = new _1669.Solution2();
}

@Test
Expand All @@ -31,5 +34,12 @@ public void test1() {
LinkedListUtils.printList(actual);
assertEquals(expected, actual);
}

}
@Test
public void test2() {
l1 = ListNode.createSinglyLinkedList(Arrays.asList(0, 1, 2, 3, 4, 5));
l2 = ListNode.createSinglyLinkedList(Arrays.asList(1000000,1000001,1000002));
a = 3;
b = 4;
assertEquals(ListNode.createSinglyLinkedList(Arrays.asList(0,1,2,1000000,1000001,1000002,5)), solution2.mergeInBetween(l1, a, b, l2));
}
}