https://leetcode-cn.com/problems/swap-nodes-in-pairs/
/**
* 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 preHead = new ListNode(0, head);
ListNode cur = head;
ListNode pre = preHead;
while(cur != null){
if(cur.next == null){
return preHead.next;
}
else{
ListNode two = cur.next;
ListNode three = two.next;
pre.next = two;
two.next = cur;
cur.next = three;
pre = cur;
cur = three;
}
}
return preHead.next;
}
}