package com.fishercoder.solutions;

import com.fishercoder.common.classes.ListNode;

/**
 * 24. Swap Nodes in Pairs
 *
 * Given a linked list, swap every two adjacent nodes and return its head.

 For example,
 Given 1->2->3->4, you should return the list as 2->1->4->3.

 Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

 */
public class _24 {

    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode second = head.next;
        ListNode third = second.next;
        second.next = head;
        head.next = swapPairs(third);
        return second;
    }

}

@@ -413,17 +413,56 @@
 s _24 %7B%0A
-%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -508,16 +508,20 @@
         
+  
+  
 if (head
@@ -561,24 +561,28 @@
             
+    
 return head;
@@ -578,24 +578,28 @@
 eturn head;%0A
+    
         %7D%0A  
@@ -596,32 +596,36 @@
       %7D%0A        
+    
 ListNode second 
@@ -637,32 +637,36 @@
 d.next;%0A        
+ 
+   
 ListNode third =
@@ -687,16 +687,20 @@
         
+  
+  
 second.n
@@ -707,24 +707,28 @@
 ext = head;%0A
+    
         head
@@ -753,16 +753,20 @@
 third);%0A
+    
         
@@ -780,16 +780,26 @@
 second;%0A
+        %7D%0A
     %7D%0A%0A%7D

refactor 24