周梦康 发表于 2015-06-22 3202 次浏览 标签 : 计算机基础剑指 offer数据结构
struct ListNode {
    int val;
    ListNode* pNext;
};

void DeleteNode(ListNode** pListHead,ListNode* pToBeDeleted){
    if(!pListHead || !pToBeDeleted){
        return;
    }

    if(pToBeDeleted->pNext != NULL){
        ListNode* nextNode = pToBeDeleted->pNext;
        pToBeDeleted->val = nextNode->val;
        pToBeDeleted->pNext = nextNode->pNext;

        delete nextNode;
        nextNode = NULL;
    }else if(*pListHead == pToBeDeleted){
        delete pToBeDeleted;
        pToBeDeleted = NULL;
        *pListHead = NULL;
    }else{
        // 链表中有多个节点,删除尾节点
        ListNode* pNode = *pListHead;
        while(pNode->pNext != pToBeDeleted){
            pNode = pNode->pNext;
        }
        pNode->pNext = NULL;
        delete pToBeDeleted;
        pToBeDeleted = NULL;
    }
}

评论列表