题解 | #交互链表节点#交换相邻节点的数值即可
交互链表节点
https://www.nowcoder.com/practice/64a7ee97b02042c189b35e3608e336ae
/**
* 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;
}
};
