点击查看代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode cur = dummy;
while(cur.next != null&&cur.next.next!=null) {
ListNode node1 = cur.next;
ListNode node2 = cur.next.next;
ListNode temp = cur.next.next.next;
cur.next = node2;
node2.next = node1;
node1.next = temp;
cur = node1;
}
return dummy.next;
}
}