Sword finger Offer delete node of linked list

Article directory

1. topic 1

Delete the linked list node in O(1) time.
Given the head pointer and a node pointer of one-way linked list, a function is defined to delete the node in O(1) time.

1.1 topic analysis

To delete a node in a single linked list, the general method is to start from the head node of the linked list, search for the node to be deleted in sequence, and delete the node in the linked list, but the time complexity of this method is O(n), which is inconsistent with the requirements of the title.

We can copy the content of the next node to be deleted to the node to be deleted to overwrite the original content, then point the pointer of the node to be deleted to the next node to be deleted, and then delete the next node to be deleted. The effect is to delete the node to be deleted.

1.2 code implementation

// Define node type of single chain table
typedef struct ListNode
{
    int data;                                   // Data domain
    struct ListNode* next;                      // Pointer domain
}ListNode;

void DeleteNode(ListNode** pListHead, ListNode* pDelete)
{
    // The head node of the linked list or the node to be deleted is NULL, exit directly
    if (pListHead == NULL || pDelete == NULL) {
        return;
    }
 
    if (pDelete->next != NULL) {                // The node to be deleted is not a tail node
        ListNode* pNext = pDelete->next;
        pDelete->data = pNext->data;            // Copy the data of the next node of the node to be deleted to the node to be deleted
        pDelete->next = pNext->next;            // Point the next of the node to be deleted to the next of the node to be deleted

        free(pNext);                            // Free memory
        pNext = NULL;                           // Set the pointer to NULL to prevent "wild pointer"
    } else if (*pListHead == pDelete) {         // There is only one node in the linked list. Delete the head node (also the tail node)
        free(pDelete);                         
        pDelete = NULL;                        
        *pListHead = NULL;                      // Set the chain header node to NULL
    } else {                                    // Delete the tail node when there are multiple nodes in the linked list
        ListNode* pNode = *pListHead;

        while (pNode->next != pDelete) {        // Traverse to the pre order node of the node to be deleted in order
            pNode = pNode->next;
        }

        pNode->next = NULL;                     // Set the next of the predecessor node to be deleted to NULL

        free(pDelete);
        pDelete = NULL;
    }
}

2. topic 2

Delete duplicate nodes in the list.

2.1 topic analysis

If the value of the current node is the same as that of the next node, then they are duplicate nodes and need to be deleted. In order to ensure that the deleted linked list is still connected, the previous node of the current node should be connected with the node whose later value is larger than the current node.

2.2 code implementation

// Define node type of single chain table
typedef struct ListNode
{
    int data;                                   // Data domain
    struct ListNode* next;                      // Pointer domain
}ListNode;

void DeleteDuplication(ListNode** pListHead)
{
    // The address of the head node or the head node of the linked list is NULL, so it exits directly
    if (pListHead == NULL || *pListHead == NULL) {
        return;
    }

    ListNode* pPreNode = NULL;                  // Set the previous node of the current node to NULL
    ListNode* pNode = *pListHead;               // Current node refers to head node

    while (pNode != NULL) {                     // Current node is not NULL
        ListNode* pNext = pNode->next;          // next of current node
        bool needDelete = false;                // Whether to delete or not, yes is true, no is false

        // If the next of the current node is not NULL and the data of the next of the current node is equal to the data of the current node, there are duplicate nodes, which need to be deleted
        if (pNext != NULL && pNext->data == pNode->data) {
            needDelete = true;
        }

        if (!needDelete) {                      // If there are no duplicate nodes, traverse backward
            pPreNode = pNode;
            pNode = pNode->next;
        } else {                                // If there are duplicate nodes
            int dataValue = pNode->data;        // Assign data of current node to dataValue
            ListNode* pDelete = pNode;          // Make the node to be deleted point to the current node

            // Delete duplicate nodes
            while (pDelete != NULL && pDelete->data == dataValue) {
                pNext = pDelete->next;

                free(pDelete);                  // Free memory
                pDelete = NULL;                 // Set the pointer to NULL to prevent "wild pointer"

                pDelete = pNext;
            }

            if (pPreNode == NULL) {             // There are duplicate nodes from the head node of the linked list. At this time, pPreNode is NULL
                *pListHead = pNext;
            } else {
                pPreNode->next = pNext;
            }

            pNode = pNext;
        }
    }
}
Published 24 original articles, won praise 26, visited 10000+
Private letter follow

Posted by lancet2003 on Mon, 03 Feb 2020 06:48:13 -0800