(1)两两交换,递归求解
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode *n_head=nullptr;
if(head==nullptr) return nullptr;
if(head->next!=nullptr) {
ListNode *temp=head->next->next;
head->next->next=head;
n_head=head->next;
head->next=swapPairs(temp);
} else return head;
return n_head;
}
};