数据结构练习(24)在O(1)时间删除链表结点

http://zhedahht.blog.163.com/blog/static/254111742007112255248202/

struct Listnode {
    int m_Key;
    Listnode *m_Next;
};

void DeleteNode(Listnode *head, Listnode *deletenode)
{
    if (!head || !deletenode)
        return ;

    if (deletenode->m_pNext)
    {
        Listnode *p = deletenode->m_Next;
        deletenode->m_Key = p->m_Key;
        deletenode->m_Next = p->m_Next;
        delete p;
        p = nullptr;
    }
    else
    {
        Listnode *p = head;
        while (p->m_Next != deletenode)
            p = p->m_Next;

        p->m_Next = nullptr;
        delete deletenode;
        deletenode = nullptr;
    }
}

 

posted @ 2012-12-17 14:29  kedebug  阅读(199)  评论(0编辑  收藏  举报