Delete node of linked list: given the head pointer and a node pointer of one-way linked list, define a function to delete the node in O (1) time.
Analysis: because the topic directly gives the node to be deleted, if we want to delete the given node within the O (1) time, we can overwrite the next node to be deleted with this node, and then delete the next node. But there are two special cases. First, when there is only one node in the whole list, we only assign the chain as nullptr. Second, we can do the following If this node is the last node of the list, but the list does not have only one node, then you have to use the order to traverse to find the previous node of this node and then delete this node.
#include<iostream> using namespace std; //Delete node in O (1) time struct ListNode{ int value; ListNode* next; }; void DeleteNode(ListNode** pListNode,ListNode *pToBeDeleted){ if(!pListNode || !pToBeDeleted) return ; //The node to be deleted is not a tail node if(pToBeDeleted->next != nullptr){ //Then overwrite the previous node with the latter one, and delete the latter one ListNode* pNext = pToBeDeleted -> next; pToBeDeleted->value = pNext->value; pToBeDeleted->next = pNext->next; delete pNext; } else if(*pListNode == pToBeDeleted) { //If the list has only one node delete pToBeDeleted; pToBeDeleted = nullptr; pListNode = nullptr; }else{ //If there are multiple nodes, and then the last one is deleted, search in order ListNode* p = *pListNode; while(p->next!=pToBeDeleted) p = p->next; p->next = nullptr; delete pToBeDeleted; pToBeDeleted = nullptr; } }
Complexity analysis:
*Time complexity: for n-1 non tail nodes, O (1) time can be used to delete. For a tail node, O (n) can be used. To sum up, the average time complexity calculated is [(n-1)O(1) + (n)]/n to get o (1).
Delete duplicate nodes in the linked list: in a sorted linked list, there are duplicate nodes. Please delete the duplicate nodes in the linked list. The duplicate nodes are not reserved and the chain header pointer is returned. For example, the list 1 - > 2 - > 3 - > 3 - > 4 - > 4 - > 5 is 1 - > 2 - > 5 after being processed.
Analysis: to delete duplicate nodes, you can define a flag bit. First, use p to traverse the list, use pre to save its precursor node, and pNext to represent the next node of P. to determine whether to use pNext & & pNext - > Val = = P - > Val repeatedly, we will set the flag bit to true, if false, we will move the pointer directly, if true, we will have to cycle, and repeatedly compare the value of the node. Whether it is equal to value, and then delete it. Finally, pay special attention that when pre is nullptr, it means to delete it from the chain head. Then you need to set pHead=p, if not, you need to directly set pre - > next = P.
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; */ class Solution { public: ListNode* deleteDuplication(ListNode* pHead) { ListNode* pre = nullptr,*p = pHead,*pNext = nullptr,*ret = pHead; bool toDelete = false; while(p!=nullptr){ toDelete = false; pNext = p->next; if(pNext && pNext->val == p->val){ toDelete = true; } if(!toDelete){ //If not, move the pointer directly pre = p; p = p->next; }else{ //If it's the same, you have to delete all the duplicate nodes. int value = p->val; while(p && p->val == value){ //Delete all duplicate nodes //But when deleting, you need to save the next node, otherwise the chain will be broken. pNext = p->next; delete p; p = pNext; } if(pre!=nullptr){ pre->next = p; }else{ ret = p; } } } return ret; } };