package leetcode;

/**
 * https://leetcode.com/problems/swapping-nodes-in-a-linked-list/
 */
public class Problem1721 {
    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 swapNodes(ListNode head, int k) {
        // TODO
        return null;
    }

    private static ListNode build(int[] array) {
        ListNode head = new ListNode(array[0]);
        for (int i = 1; i < array.length; i++) {
            head.next = new ListNode(array[i]);
            head = head.next;
        }
        return head;
    }

    private static void print(ListNode head) {
        for (ListNode n = head; n != null; n = n.next) {
            System.out.print(n.val + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Problem1721 prob = new Problem1721();
        print(prob.swapNodes(build(new int[]{1,2,3,4,5}), 2)); // [1,4,3,2,5]
        print(prob.swapNodes(build(new int[]{7,9,6,6,7,8,3,0,9,5}), 5)); // [7,9,6,6,8,7,3,0,9,5]
        print(prob.swapNodes(build(new int[]{1}), 1)); // [1]
        print(prob.swapNodes(build(new int[]{1,2}), 1)); // [2,1]
        print(prob.swapNodes(build(new int[]{1,2,3}), 2)); // [1,2,3]
    }
}

@@ -12,16 +12,68 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A * h
@@ -455,170 +455,287 @@
   %7D%0A
-    %7D%0A%0A    public ListNode swapNodes(ListNode head, int k) %7B%0A        // TODO%0A        return null;
+%0A        @Override%0A        public String toString() %7B%0A            return %22%22 + val;%0A        %7D%0A    %7D%0A%0A    public ListNode swapNodes(ListNode head, int k) %7B%0A        List%3CListNode%3E nodes = new ArrayList%3C%3E();%0A        // Destroy the linkage.
 %0A    
-%7D%0A%0A
     
-private static ListNode build(int%5B%5D array) %7B%0A
+ListNode prev = null;%0A 
        
- 
+for (
 List
@@ -743,84 +743,83 @@
 ode 
-head = new ListNode(array%5B0%5D);%0A        for (int i = 1; i %3C array.length; i++
+node = head; node != null; node = node.next) %7B%0A            if (prev != null
 ) %7B%0A
@@ -830,20 +830,24 @@
         
-head
+    prev
 .next = 
@@ -851,145 +851,164 @@
  = n
-ew ListNode(array%5Bi%5D);%0A            head = head.next;%0A        %7D%0A        return head
+ull;%0A            %7D%0A            nodes.add(node);%0A            prev = node;%0A        %7D%0A        int leftIndex = k - 1
 ;%0A    
-%7D%0A%0A
     
-private static void print(ListNode head) %7B
+int rightIndex = nodes.size() - k;
 %0A   
@@ -1004,37 +1004,32 @@
 () - k;%0A        
-for (
 ListNode n = hea
@@ -1025,470 +1025,305 @@
 ode 
-n = head; n != null; n = n.next) %7B%0A            System.out.print(n.val + %22 %22);%0A        %7D%0A        System.out.println(
+tmp = nodes.get(leftIndex);%0A        nodes.set(leftIndex, nodes.get(rightIndex));%0A        nodes.set(rightIndex, tmp
 );%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1721 prob = new Problem1721();%0A        print(prob.swapNodes(build(new int%5B%5D%7B1,2,3,4,5%7D), 2)); // %5B1,4,3,2,5%5D%0A        print(prob.swapNodes(build(new int%5B%5D%7B7,9,6,6,7,8,3,0,9,5%7D), 5)); // %5B7,9,6,6,8,7,3,0,9,5%5D%0A        print(prob.swapNodes(build(new int%5B%5D%7B1%7D), 1)); // %5B1%5D%0A
+// Build the linkage.%0A        prev = null;%0A        for (ListNode node : nodes) %7B%0A            if (prev != null) %7B%0A                prev.next = node;%0A            %7D%0A   
         
 prin
@@ -1322,135 +1322,63 @@
     
+ 
 pr
-int(prob.swapNodes(build(new int%5B%5D%7B1,2%7D), 1)); // %5B2,1%5D%0A        print(prob.swapNodes(build(new int%5B%5D%7B1,2,3%7D), 2)); // %5B1,2,3%5D
+ev = node;git%0A        %7D%0A        return nodes.get(0);
 %0A   

Solve problem 1721