学习到的有两点,leetcode好像不设置哨兵节点,需要的话要自己额外设置。
点击查看代码
ListNode*dummy=new ListNode(0);
点击查看代码
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(!head||!head->next){return head;}
ListNode*p=head->next;
head->next=swapPairs(p->next);
p->next=head;
return p ;
}
};