@@ -0,0 +1,1026 @@
+package leetcode;%0A%0Apublic class SwapNodesinPairs %7B%0A%09public static ListNode swapPairs(ListNode head) %7B%0A%09%09if (head == null) %7B%0A%09%09%09return head;%0A%09%09%7D%0A%09%09ListNode p = new ListNode(0);%0A%09%09p.next = head;%0A%09%09ListNode cur = p;%0A%09%09while (cur.next != null && cur.next.next != null) %7B%0A%09%09%09ListNode q1 = cur.next;%0A%09%09%09ListNode q2 = cur.next.next;%0A%09%09%09q1.next = q2.next;%0A%09%09%09q2.next = q1;%0A%09%09%09cur.next = q2;%0A%09%09%09cur = q1;%0A%09%09%7D%0A%09%09return p.next;%0A%09%7D%0A%0A%09public static ListNode MakeLinkedList(int d%5B%5D) %7B%0A%09%09ListNode head = new ListNode(d%5B0%5D);%0A%09%09ListNode pre = head;%0A%09%09ListNode cur = head;%0A%09%09for (int i = 1; i %3C d.length; i++) %7B%0A%09%09%09cur = new ListNode(d%5Bi%5D);%0A%09%09%09pre.next = cur;%0A%09%09%09pre = cur;%0A%09%09%7D%0A%09%09return head;%0A%09%7D%0A%0A%09public static void printLink(ListNode link) %7B%0A%09%09while (link != null) %7B%0A%09%09%09System.out.print(link.val + %22--%3E%22);%0A%09%09%09link = link.next;%0A%09%09%7D%0A%09%09System.out.println();%0A%09%7D%0A%0A%09public static void main(String%5B%5D args) %7B%0A%09%09int d%5B%5D = %7B 1%7D;%0A%09%09ListNode link = MakeLinkedList(d);%0A%09%09// printLink(link);%0A%09%09ListNode result = swapPairs(link);%0A%09%09printLink(result);%0A%09%7D%0A%0A%7D%0A

swap node