Sword Finger offer-Chapter III High-quality code (O (1) time to delete a linked list node)

Source: Internet
Author: User

Title: Given one-way list of head pointers and a node pointer, define a function at O (1) Time to delete the node, linked list nodes and functions are defined as follows: struct Listnode{int m_nvalue; Listnode* M_pvalue;}; void Deletenode (listnode** plistnode,listnode * ptobedeleted) {}

Idea: Assign the value of the next node of the node you want to delete to I, delete the next node, but consider that the deleted node is the last node and that the list has more than one node and that the list has only one node, which is both the head node and the tail node.

C + + code:

#include <iostream>using namespacestd; structlistnode{intM_nvalue; ListNode*M_pnext;};//Create a linked listlistnode* Createlink (intA[],intk) {ListNode* head=null,*q=NULL;  for(intI=0; i<k;i++) {ListNode* pnew=NewListNode (); Pnew->m_nvalue=A[i]; Pnew->m_pnext=NULL; if(head==NULL) {Head=pnew; Q=pnew; }          Else{Q->m_pnext=pnew; Q=q->M_pnext; }      }      returnHead; }  //Print List from beginning to endvoidPrintlink (ListNode *phead) {ListNode*p=Phead;  while(p) {cout<<p->m_nValue<<" "; P=p->M_pnext; } cout<<Endl; }  voidDeletenode (listnode** plisthead,listnode*ptobedeleted) {    if(!plisthead| |!ptobedeleted) {        return; }    //Node behind the node to be removed    if(ptobedeleted->m_pnext!=NULL) {ListNode* pnext=ptobedeleted->M_pnext; Ptobedeleted->m_nvalue=pnext->M_nvalue; Ptobedeleted->m_pnext=pnext->M_pnext; DeletePnext; Pnext=NULL; }    //the node to be deleted is the head node and the last one .    Else if(*plisthead==ptobedeleted) {        Deleteptobedeleted; Ptobedeleted=NULL; *plisthead=NULL; }    Else{ListNode* pnode=*Plisthead;  while(pnode->m_pnext!=ptobedeleted) {Pnode=pnode->M_pnext; } Pnode->m_pnext=NULL; Deleteptobedeleted; Ptobedeleted=NULL; }}voidMain () {inta[]={1,2,3}; ListNode* Head=createlink (A,3);      Printlink (Head); ListNode* pnode=Head;  while(pnode->m_nvalue!=a[1]&&pnode) Pnode=pnode->M_pnext; if(!Pnode) {printf ("There are no such nodes"); }    ElseDeletenode (&Head,pnode);    Printlink (Head); cout<<Endl;} 

Java code:

 Public classSinglelinklist { Public  Static classListNode { Public intM_nvalue;  PublicListNode M_pnext; }    //Create a linked list     Public  StaticListNode Createlink (intA[],intk) {ListNode Head=NULL, q=NULL;  for(inti=0;i<k;i++) {ListNode pnew=NewListNode (); Pnew.m_nvalue=A[i]; Pnew.m_pnext=NULL; if(head==NULL) {Head=pnew; Q=pnew; }              Else{Q.m_pnext=pnew; Q=Q.m_pnext; }          }          returnHead; }      //Print List from beginning to end     Public Static  voidPrintlink (ListNode phead) {ListNode P=Phead;  while(P! =NULL) {System.out.print (P.m_nvalue+" "); P=P.m_pnext; } System.out.println ("\ n"); }       Public Static voiddeletenode (ListNode plisthead,listnode ptobedeleted) {if(plisthead==NULL|| ptobedeleted==NULL)        {            return; }        //Node behind the node to be removed        if(ptobedeleted.m_pnext!=NULL) {ListNode Pnext=Ptobedeleted.m_pnext; Ptobedeleted.m_nvalue=Pnext.m_nvalue; Ptobedeleted.m_pnext=Pnext.m_pnext; }        //the node to be deleted is the head node and the last one .        Else if(plisthead==ptobedeleted) {ptobedeleted=NULL; Plisthead=NULL; }        Else{ListNode Pnode=Plisthead;  while(pnode.m_pnext!=ptobedeleted) {Pnode=Pnode.m_pnext; } Pnode.m_pnext=NULL; Ptobedeleted=NULL; }    }     Public Static voidMain (string[] args) {inta[]={1,2,3}; ListNode Head=createlink (a,3);          Printlink (Head); ListNode Pnode=Head;  while(pnode.m_nvalue!=a[1]&&pnode!=NULL) Pnode=Pnode.m_pnext; if(pnode==NULL) {System.out.println ("There is no such node"); }        ElseDeletenode (Head,pnode);        Printlink (Head); System.out.println ("\ n"); }     }

Sword Finger offer-Chapter III High-quality code (O (1) time to delete a linked list node)

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.