又是基本功,链表的基本操作。
注意犯错的地方
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy;
while (head != null && head.next != null) {
ListNode temp = head.next.next;
prev.next = head.next;
head.next.next = head;
////////
head.next = temp;
////////
prev = head;
head = temp;
}
return dummy.next;
}
}