24 交换链表中相连的节点

    ListNode *swapPairs(ListNode *head) {
        if (head == nullptr || head->next == nullptr)
            return head;
        ListNode *it = head;
        head = head->next;
        it->next = head->next;
        head->next = it;
        while (it->next != nullptr && it->next->next != nullptr) {
            ListNode *temp = it->next;
            it->next = it->next->next;
            temp->next = it->next->next;
            it->next->next = temp;
            it = it->next->next;
        }
        return head;
    }
posted @ 2018-12-25 11:54  INnoVation-V2  阅读(132)  评论(0编辑  收藏  举报