package leetcode;

/**
 * https://leetcode.com/problems/merge-in-between-linked-lists/
 */
public class Problem1669 {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        // TODO
        return null;
    }

    private static ListNode build(int[] array) {
        ListNode root = null;
        ListNode current = null;
        for (int i = 0; i < array.length; i++) {
            if (i == 0) {
                root = new ListNode(array[i]);
                current = root;
            } else {
                current.next = new ListNode(array[i]);
                current = current.next;
            }
        }
        return root;
    }

    private static String toString(ListNode node) {
        StringBuilder sb = new StringBuilder();
        for (ListNode n = node; n != null; n = node.next) {
            if (sb.length() == 0) {
                sb.append(node.val);
            } else {
                sb.append(",").append(node.val);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Problem1669 prob = new Problem1669();
        System.out.println(toString(prob.mergeInBetween(
            build(new int[]{0, 1, 2, 3, 4, 5}),
            3, 4,
            build(new int[]{1000000, 1000001, 1000002})))); // [0,1,2,1000000,1000001,1000002,5]
        System.out.println(toString(prob.mergeInBetween(
            build(new int[]{0, 1, 2, 3, 4, 5, 6}),
            2, 5,
            build(new int[]{0, 1, 1000000, 1000001, 1000002, 1000003, 1000004, 6})))); // [1000000,1000001,1000002,1000003,1000004]
    }
}

@@ -499,114 +499,26 @@
     
-// TODO%0A        return null;%0A    %7D%0A%0A    private static ListNode build(int%5B%5D array) %7B%0A        ListNode root
+ListNode tailList2
  = n
@@ -522,32 +522,37 @@
 = null;%0A        
+for (
 ListNode current
@@ -548,69 +548,40 @@
 ode 
-current = null;%0A        for (int i = 0; i %3C array.length; i++
+n = list2; n != null; n = n.next
 ) %7B%0A
@@ -600,14 +600,22 @@
 if (
-i == 0
+n.next == null
 ) %7B%0A
@@ -634,395 +634,186 @@
     
-root = new ListNode(array%5Bi%5D);%0A                current = root;%0A            %7D else %7B%0A                current.next = new ListNode(array%5Bi%5D);%0A                current = current.next;%0A            %7D%0A        %7D%0A        return root;%0A    %7D%0A%0A    private static String toString(ListNode node) %7B%0A        StringBuilder sb = new StringBuilder();%0A        for (ListNode n = node; n != null; n = node.next
+tailList2 = n;%0A                break;%0A            %7D%0A        %7D%0A        int i = 0;%0A        ListNode prev = null;%0A        for (ListNode cur = list1; cur != null; cur = cur.next, i++
 ) %7B%0A
@@ -832,24 +832,14 @@
 if (
-sb.length()
+i
  == 
-0
+a
 ) %7B%0A
@@ -858,27 +858,25 @@
     
-sb.append(node.val)
+prev.next = list2
 ;%0A  
@@ -890,15 +890,8 @@
    %7D
- else %7B
 %0A   
@@ -903,58 +903,21 @@
     
-    sb.append(%22,%22).append(node.val);%0A            %7D
+if (i == b) %7B
 %0A   
@@ -921,26 +921,24 @@
         
-%7D%0A
         
 return s
@@ -933,232 +933,57 @@
     
-return sb.toString();%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1669 prob = new Problem1669();%0A        System.out.println(toString(prob.mergeInBetween(%0A            build(new int%5B%5D%7B0, 1, 2, 3, 4, 5%7D),
+tailList2.next = cur.next;%0A                break;
 %0A   
@@ -991,21 +991,17 @@
         
-3, 4,
+%7D
 %0A       
@@ -1009,350 +1009,51 @@
     
-build(new int%5B%5D%7B1000000, 1000001, 1000002%7D)))); // %5B0,1,2,1000000,1000001,1000002,5%5D%0A        System.out.println(toString(prob.mergeInBetween(%0A            build(new int%5B%5D%7B0, 1, 2, 3, 4, 5, 6%7D),%0A            2, 5,%0A            build(new int%5B%5D%7B0, 1, 1000000, 1000001, 1000002, 1000003, 1000004, 6%7D)))); // %5B1000000,1000001,1000002,1000003,1000004%5D
+prev = cur;%0A        %7D%0A        return list1;
 %0A   

Solve problem 1669