/**
* 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) {
if (head == null) {
return null;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode ptr = dummy;
while (ptr.next != null && ptr.next.next != null) {
ListNode temp = ptr.next;
ptr.next = temp.next;
temp.next = ptr.next.next;
ptr.next.next = temp;
ptr = temp;
}
return dummy.next;
}
}