【LeetCode-24】Swap Nodes in Pairs(C++)

来源:互联网 发布:大众点评数据 编辑:程序博客网 时间:2024/06/07 09:17

交换链表中每两个相邻的节点 不能改变节点的值 智能改变节点。

递归法:

class Solution {public:    ListNode* swapPairs(ListNode* head) {        if(head==NULL||head->next==NULL)            return head;        ListNode* temp1=head;        ListNode* temp2=head->next->next;        head=head->next;        head->next=temp1;        head->next->next=swapPairs(temp2);        return head;    }};


0 0
原创粉丝点击