此题建议画图, 绝对要画图, 画完都不太用想的
代码
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode A = dummy;
ListNode B = head;
while(B != null && B.next != null){
ListNode C = B.next;
A.next = B.next;
B.next = C.next;
C.next = B;
A = B;
B = B.next;
}
return dummy.next;
}
}
图好评