leetcode 24 Swap Nodes in Pairs C++

来源:互联网 发布:bjd娃娃6分娃淘宝 编辑:程序博客网 时间:2024/06/03 06:06

注意只能换节点,不能换值。


class Solution {public:    ListNode* swapPairs(ListNode* head) {        ListNode *cur = head;        ListNode *preCur = NULL;        if(head&&head->next) head = head->next;                while(cur){            if(cur->next){                ListNode *tmp = cur->next->next;                cur->next->next = cur;                if(preCur) preCur->next = cur->next;                preCur = cur;                cur->next = tmp;            }            cur = cur->next;        }        return head;    }};


0 0
原创粉丝点击