执行用时 :0 ms, 在所有 Java 提交中击败了 100.00% 的用户
内存消耗 :34.2 MB, 在所有 Java 提交中击败了 86.60% 的用户
- public ListNode swapPairs(ListNode head) {
- if (head == null || head.next == null) return head;
- ListNode a = head, b = head.next, c, last = null;
- head = b;
- while (a != null && b != null) {
- c = b.next;
- b.next = a;
- a.next = c;
- if (last != null) last.next = b;
- last = a;
- a =c;
- if (a != null) b = a.next;
- }
- return head;
- }
来源: http://www.bubuko.com/infodetail-3210146.html