咋创建自己的网站,兰州免费网站建设,做百度推广一个月多少钱,wordpress如何安装插件参考链接#xff1a;代码随想录#xff1a;LeetCode24.两两交换链表中的节点 我这里使用了3个变量进行暴力交换#xff0c;简单快捷#xff01;但是有一点想不明白#xff0c;return这里只能写dh-next,写返回head就结果不对了#xff01;但是后面又想明白了#xff…参考链接代码随想录LeetCode24.两两交换链表中的节点 我这里使用了3个变量进行暴力交换简单快捷但是有一点想不明白return这里只能写dh-next,写返回head就结果不对了但是后面又想明白了这里是因为交换了节点所以只能写dh-next
class Solution {
public:ListNode* swapPairs(ListNode* head) {ListNode* tmpnullptr,*tmp1nullptr,*tmp2nullptr,*dhnew ListNode(0,head),*curnullptr;if(!head||!head-next){return head;}curdh;while(cur-nextcur-next-next){tmpcur-next;tmp1cur-next-next;tmp2cur-next-next-next;cur-nexttmp1;tmp1-nexttmp;tmp-nexttmp2;curtmp;}return dh-next;}
};