1 struct ListNode 2 { 3 int data; 4 ListNode* next; 5 }; 6 void AddToTail(ListNode** pHead,int v){ 7 ListNode* pNew = new ListNode(); 8 pNew->data = v; 9 pNew->next = NULL; 10 if(*pHead == NULL) 11 *pHead = pNew; 12 else{ 13 ListNode* pNode = *pHead; 14 while(pNode->next!=NULL) 15 pNode = pNode->next; 16 pNode->next = pNew; 17 } 18 } 19 void RemoveNode(ListNode** pHead,int v) 20 { 21 if(pHead == NULL || *pHead == NULL) 22 return; 23 ListNode* pToBeDeleted = NULL; 24 if((*pHead)->data == v){ 25 pToBeDeleted = *pHead; 26 *pHead = (*pHead)->next; 27 }else{ 28 ListNode* pNode = *pHead; 29 while(pNode->next !=NULL && pNode->next->data !=v) 30 pNode = pNode->next; 31 if(pNode->next!=NULL && pNode->next->data ==v){ 32 pToBeDeleted = pNode->next; 33 pNode->next = pNode->next->next; 34 } 35 } 36 if(pToBeDeleted!=NULL){ 37 delete pToBeDeleted; 38 pToBeDeleted = NULL; 39 } 40 }