@@ -0,0 +1,1832 @@
+/*%0A * https://leetcode.com/problems/swap-nodes-in-pairs/%0A * Given a linked list, swap every two adjacent nodes and return its head.%0A * For example,%0A * Given 1-%3E2-%3E3-%3E4, you should return the list as 2-%3E1-%3E4-%3E3.%0A * Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed%0A. */%0Aclass ListNode %7B%0A    int val;%0A    ListNode next;%0A    ListNode(int x) %7B val = x; %7D%0A%7D%0A%0A%0Aclass Solution %7B%0A    public ListNode swapPairs(ListNode head) %7B%0A        if(head == null)%0A            return head;%0A        ListNode slow = head;%0A        ListNode fast = head.next;%0A        %0A        while(slow != null && fast != null) %7B%0A            int tmp = slow.val;%0A            slow.val = fast.val;%0A            fast.val = tmp;%0A            if(fast.next != null)%0A                fast = fast.next;%0A            else%0A                break;%0A            slow = fast;%0A            %0A            if(fast.next != null)%0A                fast = fast.next;%0A            else%0A                break;%0A        %7D%0A  %0A        return head;%0A    %7D%0A    public void print(ListNode head) %7B%0A      while(head != null) %7B%0A        System.out.println(head.val);%0A        head = head.next;%0A      %7D%0A    %7D%0A    public ListNode createList(int num) %7B%0A        ListNode head = null;%0A        ListNode tail = head;%0A        for(int i = 0; i %3C num; i++) %7B%0A            ListNode tmp = new ListNode(i);%0A            if(head == null) %7B%0A                head = tail = tmp;%0A            %7D else %7B%0A              tail.next = tmp;%0A              tail = tmp;%0A            %7D%0A        %7D%0A        return head;%0A    %7D%0A%7D%0A%0Aclass Main %7B%0A  public static void main(String%5B%5D args) %7B%0A    Solution solution = new Solution();%0A    ListNode head = solution.createList(11);%0A    solution.print(head);%0A    System.out.println();%0A    solution.swapPairs(head);%0A    solution.print(head);%0A  %7D%0A%7D%0A

Add solution 24. Swap Nodes in Pairs