LeetCode 24. 两两交换链表中的节点 【时间击败100.00%】 【内存击败86.60%】

 
 1 public ListNode swapPairs(ListNode head) {
 2         if (head == null || head.next == null) return head;
 3         ListNode a = head, b = head.next, c, last = null;
 4         head = b;
 5         while (a != null && b != null) {
 6             c = b.next;
 7             b.next = a;
 8             a.next = c;
 9             if (last != null) last.next = b;
10             last = a;
11 
12             a =c;
13             if (a != null) b = a.next;
14         }
15         return head;
16     }

 

posted @ 2019-09-23 18:49  dodoBehind  阅读(149)  评论(0编辑  收藏  举报