13. Delete a linked list node in O(1) time

  Question : Given the head pointer of a singly linked list and a node pointer, define a function to delete the node in O(1) time. The definition of linked list nodes and functions is as follows:
  
  idea : set the node pointer to be deleted as i, find the next node j of the node, then copy the content of j to i, and point the pointer of i to the value of j Next node, and then delete the j node. The effect of this is to delete the i node;
  but if the node to be deleted is at the end of the linked list, then it has no next node. Therefore, it is also necessary to start from the head node of the linked list, traverse sequentially to obtain the pre-order node of the node, and then complete the deletion operation;
  if there is only one node in the linked list, after deleting, the head node of the linked list should be set to NULL .

  Test cases :
  1. Functional test: head, tail, middle node, unique node
  2. Special input test: NULL pointer

#include<iostream>
#include<cstdio>
using namespace std;

struct ListNode
{
    int             m_nValue;
    ListNode*       m_pNext;
};

//创建链表结点
ListNode* CreateListNode(int value)
{
    ListNode* pNode = new ListNode();
    pNode->m_nValue = value;
    pNode->m_pNext = NULL;

    return pNode;
}

//连接链表节点
void ConnectListNodes(ListNode* pCurrent, ListNode* pNext)
{
    if (pCurrent == NULL)
    {
        cout << "连接错误" << endl;
        return;
    }

    pCurrent->m_pNext = pNext;
}

//打印链表
void PrintList(ListNode* pHead)
{
    ListNode* pNode = pHead;
    while (pNode != NULL)
    {
        cout << pNode->m_nValue << " ";
        pNode = pNode->m_pNext;
    }
    cout << endl;
}

//销毁链表
void DestroyList(ListNode* pHead)
{
    ListNode* pNode = pHead;
    while (pNode != NULL)
    {
        pHead = pHead->m_pNext;
        delete pNode;
        pNode = pHead;
    }
}

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

    //要删除的结点不是尾结点
    if (pToBeDeleted->m_pNext != NULL)
    {
        ListNode* pNext = pToBeDeleted->m_pNext;
        pToBeDeleted->m_nValue = pNext->m_nValue;
        pToBeDeleted->m_pNext = pNext->m_pNext;

        delete pNext;
        pNext = NULL;
    }

    //链表只有一个结点,删除头结点(也是尾结点),删除之后设NULL
    else if (*pListHead == pToBeDeleted)
    {
        delete pToBeDeleted;
        pToBeDeleted = NULL;
        *pListHead = NULL;
    }

    //链表中有多个结点,删除尾结点,遍历链表
    else
    {
        ListNode* pNode = *pListHead;
        while (pNode->m_pNext != pToBeDeleted)
        {
            pNode = pNode->m_pNext;
        }

        pNode->m_pNext = NULL;
        delete pToBeDeleted;
        pToBeDeleted = NULL;
    }
}

//测试
void test1()
{
    ListNode* pNode1 = CreateListNode(1);
    ListNode* pNode2 = CreateListNode(2);
    ListNode* pNode3 = CreateListNode(3);
    ListNode* pNode4 = CreateListNode(4);
    ListNode* pNode5 = CreateListNode(5);

    ConnectListNodes(pNode1, pNode2);
    ConnectListNodes(pNode2, pNode3);
    ConnectListNodes(pNode3, pNode4);
    ConnectListNodes(pNode4, pNode5);

    PrintList(pNode1);

    DeleteNode(&pNode1, pNode5);
    PrintList(pNode1);

    DeleteNode(&pNode1, pNode2);
    PrintList(pNode1);

    DeleteNode(&pNode1, pNode1);
    PrintList(pNode1);

    DestroyList(pNode1);
}

void test2()
{
    ListNode* pNode1 = CreateListNode(1);
    PrintList(pNode1);

    DeleteNode(&pNode1, pNode1);
    PrintList(pNode1);
}

void test3()
{
    PrintList(NULL);

    DeleteNode(NULL, NULL);
    PrintList(NULL);
}

int main()
{
    test1();
    test2();
    test3();
}


  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324377096&siteId=291194637