This is a widely spread Google interview question. it can effectively evaluate our basic programming skills, our response speed, and more importantly, our understanding of time complexity. Delete a node from the linked list. The most common practice is to start from the head node of the linked list, search for the node to be deleted in sequence, and delete it again. Because sequential search is required, the time complexity is O (n.
Given the head pointer and a node pointer of the linked list, delete the node at O (1) time. The linked list node is defined as follows:
struct ListNode{ int m_nKey; ListNode* m_pNext;};
The function declaration is as follows:
void DeleteNode(ListNode* pListHead, ListNode* pToBeDeleted);
This is a widely spread Google interview question. it can effectively evaluate our basic programming skills, our response speed, and more importantly, our understanding of time complexity.
Delete a node from the linked list. The most common practice is to start from the head node of the linked list, search for the node to be deleted in sequence, and delete it again. Because sequential search is required, the time complexity is O (n.
We need to start from the node to find the node to be deleted, because we need to get the first node of the node to be deleted. Let's try another way of thinking. We can get the next node from the given node. In this case, the next node is actually deleted. because we have obtained the first node of the actually deleted node, it can be fully implemented. Of course, before deleting a node, we need to copy the data of the next node of the given node to the given node. At this time, the time complexity is O (1 ).
The above thinking has another question: what if the deleted node is located at the end of the linked list and there is no next node? We still start from the head node of the linked list, traverse sequentially to get the forward node of the given node, and complete the delete operation. In this case, the time complexity is O (n ).
The question requires that we need to complete the deletion operation at O (1) time. is our algorithm not compliant? In fact, assume that the linked list has n nodes in total. in the case of n-1, the time complexity of our algorithm is O (1). only when the given node is at the end of the linked list, the time complexity is O (n ). Then the average time complexity [(n-1) * O (1) + O (n)]/n is still O (1 ).
Based on the previous analysis, it is not difficult to write the following code.
///////////////////////////////////////////////////////////////////////// Delete a node in a list// Input: pListHead - the head of list// pToBeDeleted - the node to be deleted///////////////////////////////////////////////////////////////////////void DeleteNode(ListNode* pListHead, ListNode* pToBeDeleted){ if(!pListHead || !pToBeDeleted) return; // if pToBeDeleted is not the last node in the list if(pToBeDeleted->m_pNext != NULL) { // copy data from the node next to pToBeDeleted ListNode* pNext = pToBeDeleted->m_pNext; pToBeDeleted->m_nKey = pNext->m_nKey; pToBeDeleted->m_pNext = pNext->m_pNext; // delete the node next to the pToBeDeleted delete pNext; pNext = NULL; } // if pToBeDeleted is the last node in the list else { // get the node prior to pToBeDeleted ListNode* pNode = pListHead; while(pNode->m_pNext != pToBeDeleted) { pNode = pNode->m_pNext; } // deleted pToBeDeleted pNode->m_pNext = NULL; delete pToBeDeleted; pToBeDeleted = NULL; }}
It is worth noting that in order to make the code look concise, the above code is based on two assumptions: (1) the given node is indeed in the linked list; (2) the given node to be deleted is not the head node of the linked list. The first assumption does not affect the robustness of the code. As for the second assumption, the code may be faulty when there is only one knot in the entire list. But this assumption is not too much, because in some linked list implementations, a virtual linked list header is created, and it is not an actual linked list node. In this way, the node to be deleted cannot be the head node of the linked list. Of course, during the interview, we can share these assumptions with the interviewer. In this way, the interviewer will still think that we are considerate.
PS: If you want to delete node A, A. next = B. Delete B and assign the value of B to A, which means that node A is deleted.
Address of this article: http://www.nowamagic.net/librarys/veda/detail/261,welcome.