package leetcode;

/**
 * https://oj.leetcode.com/problems/swap-nodes-in-pairs/
 */
public class Problem24 {
    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
        
        @Override
        public String toString() {
            return Integer.toString(val);
        }
    }

    public ListNode swapPairs(ListNode head) {
        // TODO: to be implemented
        return head;
    }
    
    private static void print(ListNode head) {
        for (ListNode n = head; n != null; n = n.next) {
            System.out.print(n + " ");
        }
        System.out.println();
    }
    
    public static void main(String[] args) {
        Problem24 prob = new Problem24();
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        print(prob.swapPairs(head));
    }
}

@@ -261,62 +261,82 @@
     
-    
+%7D%0A
 %0A    
-    @Override%0A        public String toString(
+public ListNode swapPairs(ListNode head) %7B%0A        if (head == null
 ) %7B%0A
@@ -358,29 +358,12 @@
 urn 
-Integer.toString(val)
+head
 ;%0A  
@@ -378,171 +378,257 @@
     
-%7D%0A%0A
     
-public ListNode swapPairs(ListNode head) %7B%0A        // TODO: to be implemented%0A        return head;%0A    %7D%0A    %0A    private static void print(ListNode head) %7B
+// swapping 3 an 4%0A        // 1 -%3E 2 -%3E 3 -%3E 4 -%3E 5 -%3E 6%0A        //      %5E    %5E    %5E    %5E%0A        //      %7C    %7C    %7C    %7C%0A        //      n1   n2   n3   n4%0A        ListNode n1 = null;%0A        ListNode n2 = head;%0A        ListNode n3 = head.next;
 %0A   
@@ -636,13 +636,8 @@
     
-for (
 List
@@ -646,41 +646,116 @@
 de n
+4
  = 
-head; n != null; n = n
+null;%0A        boolean once = false;%0A        while (n2 != null && n3 != null) %7B%0A            n4 = n3
 .next
-) %7B
+;
 %0A   
@@ -767,34 +767,77 @@
     
-System.out.print(n + %22 %22);
+n3.next = n2;%0A            n2.next = n4;%0A            if (n1 != null) %7B
 %0A   
@@ -841,26 +841,24 @@
         
-%7D%0A
         
 System.o
@@ -853,30 +853,30 @@
     
-System.out.println();%0A
+n1.next = n3;%0A        
     
@@ -885,50 +885,25 @@
     
-%0A
     
-public static void main(String%5B%5D args
+    if (!once
 ) %7B%0A
@@ -914,244 +914,386 @@
     
+  
-Problem24 prob = new Problem24();%0A        ListNode head = new ListNode(1);%0A        head.next = new ListNode(2);%0A        head.next
+      head = n3;%0A                once = true;%0A            %7D%0A            // restore everything back%0A            ListNode tmp = n2;%0A            n2 = n3;%0A            n3 = tmp;%0A            if (n2.next == null %7C%7C n3
 .next =
+=
  n
-ew ListNode(3);%0A        head.next.next.next = new ListNode(4);%0A        print(prob.swapPairs(
+ull) %7B%0A                break;%0A            %7D%0A            n1 = n3;%0A            n2 = n2.next.next;%0A            n3 = n3.next.next;%0A        %7D%0A        return 
 head
-))
 ;%0A  

Solve problem 24