[To] Delete a node in a single linked list with the time complexity of O (1)

Source: Internet
Author: User

Given a list of head pointers and a node pointer, delete the node at O (1) time. The linked list node is defined as follows:

struct listnode{int m_nkey; Listnode* M_pnext;};

The declaration of the function is as follows:

void Deletenode (listnode* plisthead, listnode* ptobedeleted);

This is a widely circulated Google interview questions, to examine our operation and time complexity of the list of the understanding of the problem, I can not think of a better solution, but others put the problem in this, there must be a solution. General single-linked list to delete a node, you need to know the node to delete the previous node, you need O (n) traversal time, obviously conventional thinking is not. In a closer look at the topic, a different way of thinking, since can not be in O (1) to delete the previous element of the node, but we can easily get the latter element, so that we do not assign the latter element to the node to be deleted, so that is equivalent to the deletion of the current element. Visible, this method is feasible, but if the node to be deleted is the last node, then can not follow the above thought, there is no way, can only follow the usual method of traversal, time complexity of O (n), is not meet the requirements of the topic? Probably a lot of people in this will be suspicious of their own thinking, so abandon this idea, and finally may give up the problem, this is the interesting place of the question, although see simple, but examined the analysis of the ability of judgment, whether have a strong psychological, full confidence. In fact, we analyze, still satisfies the topic request, if the deletion node is the front n-1 node, the time complexity is O (1), only the deletion node is the last one, the time complexity is O (n), so the average time complexity is: (O (1) * (n-1) + O (n))/n = O (1) ; still O (1). See the code below:

/* delete a node in a list with o (1)  * input:     pListHead - the head of list *             ptobedeleted - the node to be deleted * /struct  listnode  {    int             m_nKey;    ListNode*    m_pNext;}; Void deletenode (listnode *plisthead, listnode *ptobedeleted) {    if   (!plisthead | |  !ptobedeleted)         return;         if  (ptobedeleted->m_pnext != null)  {          ListNode *pNext = pToBeDeleted->m_pNext;         pToBeDeleted->m_pNext = pNext->m_pNext;         pToBeDeleted->m_nKey = pNext->m_nKey;         delete pnext;        pnext = null;     }    else { //to delete node as tail node          Listnode *ptemp = plisthead;        while (pTemp->m_ pnext != ptobedeleted)               ptemp = ptemp->m_pnext;        ptemp->m_pnext =  NULL;        delete pToBeDeleted;         ptobedeleted = null;    }}


[To] Delete a node in a single linked list with the time complexity of O (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.