剑指offer-O(1)时间复杂度删除单向链表指定结点

/*******************************************************************
Copyright(c) 2016, Tyrone Li
All rights reserved.
*******************************************************************/
// 作者:TyroneLi
//

/*
Q:
    给定单向链表的头指针和一个结点指针,定义一个函数
    在O(1)时间复杂度内删除。
S:
    常规的单向链表节点删除操作都需要先遍历得到要删除节点的
    前一个结点,然后在删除指定节点,但是这就需要O(n)时间复杂度,
    如果把给定节点的后一个结点的数据拷贝到指定节点,然后把指定节点
    后一个结点删除,这就可以实现在O(1)时间复杂度内删除指定节点。
*/

#include "../utils/List.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

/* 该实现基于一个基本的假设,也就是要删除的结点确实在链表中 */
void deleteListNode_quick(ListNode**pHead, ListNode*pDeletedNode)
{
    if(pHead == nullptr || pDeletedNode == nullptr)
        return;
    
    if(pDeletedNode->m_pNext != nullptr){
        ListNode*pNext = pDeletedNode->m_pNext;
        pDeletedNode->m_nValue = pNext->m_nValue;
        pDeletedNode->m_pNext = pNext->m_pNext;
        delete pNext;
        pNext = nullptr;
    }else if(*pHead == pDeletedNode)
    {
        delete pDeletedNode;
        pDeletedNode = nullptr;
        *pHead = nullptr;
    }else{
        ListNode*pNode = *pHead;
        while(pNode->m_pNext != pDeletedNode)
            pNode = pNode->m_pNext;
        
        pNode->m_pNext = nullptr;
        delete pDeletedNode;
        pDeletedNode = nullptr;
    }
}

void test_1()
{
    std::cout << "one node in list delete first node" << std::endl;
    ListNode*pHead = createListNode(1);
    printList(pHead);
    deleteListNode_quick(&pHead, pHead);
    printList(pHead);
    std::cout << "===========================" << std::endl;
}

void test_2()
{
    std::cout << "multi node in list delete head node" << std::endl;
    ListNode*pHead = createListNode(1);
    ListNode*pHead2 = createListNode(2);
    connectListNodes(pHead, pHead2);
    printList(pHead);
    deleteListNode_quick(&pHead, pHead);
    printList(pHead);
    std::cout << "===========================" << std::endl;
}

void test_3()
{
    std::cout << "multi node in list, delete among node" << std::endl;
    ListNode*pHead = createListNode(1);
    ListNode*pHead2 = createListNode(2);
    ListNode*pHead3 = createListNode(3);
    connectListNodes(pHead, pHead2);
    connectListNodes(pHead2, pHead3);
    printList(pHead);
    deleteListNode_quick(&pHead, pHead2);
    printList(pHead);
    std::cout << "===========================" << std::endl;
}

void test_4()
{
    std::cout << "multi node in list, delete tail node" << std::endl;
    ListNode*pHead = createListNode(1);
    ListNode*pHead2 = createListNode(2);
    ListNode*pHead3 = createListNode(3);
    connectListNodes(pHead, pHead2);
    connectListNodes(pHead2, pHead3);
    printList(pHead);
    deleteListNode_quick(&pHead, pHead3);
    printList(pHead);
    std::cout << "===========================" << std::endl;
}

void test_5()
{
    std::cout << "test NULL list" << std::endl;
    ListNode*pHead = nullptr;
    printList(pHead);
    deleteListNode_quick(&pHead, pHead);
    printList(pHead);
    std::cout << "============================" << std::endl;
}

void test_deleteListNode_quick()
{
    test_1();
    test_2();
    test_3();
    test_4();
    test_5();
}

int main(int argc, char**argv)
{

    test_deleteListNode_quick();

    return 0;
}
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章