/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 * @return ListNode类 */ ListNode* swapPairs(ListNode* head) { if (head == nullptr) return nullptr; if (head->next == nullptr) return head; ListNode* p = head; while (p && p->next) { ListNode* s = p->next->next; swap(p->val, p->next->val); p = s; } return head; } };