Given a linked list, swap every two adjacent nodes and return its head.

You may not modify the values in the list’s nodes, only nodes itself may be changed.

Example:

1
Given 1->2->3->4, you should return the list as 2->1->4->3.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) return head;
ListNode dummy = new ListNode(0), cur = dummy;
cur.next = head;
while (cur.next != null && cur.next.next != null) {
ListNode p1 = cur.next;
ListNode p2 = cur.next.next;
cur.next = p2;
p1.next = p2.next;
p2.next = p1;
cur = p1;
}
return dummy.next;
}
}