Sword Finger Offer Question 18-Delete the node of the linked list

Topic 1: Delete the linked list node in O(1) time

Title: Given the head pointer and a node pointer of a singly linked list, define a function to delete the node in O(1) time. The
linked list node and function are defined as follows:

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

To delete a node in a singly linked list, the normal practice is to start from the first node and proceed to the next node to judge until the node of the node that needs to be deleted is found.

As shown in the figure below:
Insert picture description here
you can start from node a and traverse sequentially, find that node h points to node i to be deleted, so point m_pNext of node h to the next node of i-j. After adjustment, safely delete node i and ensure that the list is not Disconnect
Of course, if you want to delete a node, there are other ways, such as: overwrite the content of the next node to this node, and then disconnect the subsequent nodes, then the node is deleted. The
code is as follows:

void DeleteNode(ListNode** pListHead,ListNode* pToBeDeleted)
{
    
    
	if(!pListHead || !pToBeDeleted)
		return;
	//删除的节点不是尾节点
	if(pToBeDeleted->next!=nullptr)
	{
    
    
		ListNode* pNext=pToBeDeleted->m_pNext;
		pToBeDeleted->m_nValue=pNext->m_nValue;
		pToBeDeleted->m_pNext=pNext->m_pNext;
		delete pNext;
		pNext=nullptr;
	}
	//链表只有一个节点,删除头节点(也是尾节点)
	else if(*pListHead=pToBeDeleted)
	{
    
    
		delete pToBeDeleted;
		pToBeDeleted=nullptr;
		*pListHead=nullptr;
	}
	//链表中有多个节点,删除尾节点
	else
	{
    
    
		ListNode* pNode=*pListHead;
		while(pNode->m_pNext!=pToBeDeleted)
			pNode=pNode->m_pNext;
			
		pNode->m_pNext=nullptr;
		delete pToBeDeleted;
		pToBeDeleted=nullptr;
	}
}

For the above code, the value of the next node can be overwritten to the front in O(1) time, but we still have to search sequentially, so the time complexity is O(n). So the total average time complexity is [(n-1)*O(1)+O(n)]/n, the result is still O(1)

Topic 2: Delete duplicate nodes in the linked list

Topic: How to delete duplicate nodes in a sorted linked list? For example, after the repeated nodes in the graph are deleted, the linked list is as shown in the figure
Insert picture description here

Then traverse the entire linked list from the beginning. If the value of the current node is the same as the value of the next node, then the duplicate node can be deleted. In order to ensure that the linked list after deletion is still connected, the previous node of the current node must be connected to the node whose value is greater than the current value. We need to make sure that pPreNode is always connected to the next node that is not duplicated. The
code is as follows:

void DelateDuplication(List** pHead)
{
    
    
	if(pHead==nullptr || *pHead==nullptr)
	return;
	
	ListNode* pPreNode=nullptr;
	ListNode* pNode=*pHead;
	while(pNode!=nullptr)
	{
    
    
		ListNode* pNext=pNode->m_pNext;
		bool needDeleted=false;
		if(pNext!=nullptr && pNext->m_nValue==pNode->m_nValue)
		needDelete=true;
		
		if(!needDelete)
		{
    
    
			pPreNode=pNode;
			pNode=pNode->m_pNext;
		}
		else
		{
    
    
			int value=pNode->m_nValue;
			ListNode* pToBeDel=pNode;
			while(pToBeDel!=nullptr && pToBeDel->m_nValue==value)
			{
    
    
				pNext=pToBeDel->m_pNext;
				
				delete pToBeDel;
				pToBeDel=nullptr;
				
				pToBeDel=pNext;
			}
			if(pPreNode==nullptr)
				*pHead=pNext;
			else
				pPreNode->m_pNext=pNext;
				pNode=pNext;
		}
	}
}

Guess you like

Origin blog.csdn.net/rjszz1314/article/details/104188191