/*
 struct ListNode {
 	int val;
 	struct ListNode *next;
 	ListNode(int x) : val(x), next(nullptr) {}
  };
*/
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    ListNode* swapPairs(ListNode* head) {
        ListNode* temp=head;ListNode* pre;ListNode* Next;// write code here
        if (head!=nullptr&&head->next!=nullptr) {
            head=temp->next;temp->next=head->next;head->next=temp;
        }else {
            return head;
        }
        if (head->next->next!=nullptr&&head->next->next->next!=nullptr) {
            pre=head->next;temp=pre->next;Next=temp->next;
        }else {
            return head;
        }
        while (1) {
            temp->next=Next->next;pre->next=Next;Next->next=temp;
            if (temp->next==nullptr||temp->next->next==nullptr) {
                break;
            }else {
                pre=temp;temp=temp->next;Next=temp->next;
            }
        }
        return head;
    }
};