标签:
1 struct ListNode { 2 int val; 3 ListNode *next; 4 ListNode(int x) : val(x), next(NULL) {} 5 };
1 /* Delete a node in a list with O(1) 2 * input: pListHead - the head of list 3 * pToBeDeleted - the node to be deleted 4 */ 5 6 struct ListNode 7 { 8 int m_nKey; 9 ListNode* m_pNext; 10 }; 11 12 void DeleteNode(ListNode *pListHead, ListNode *pToBeDeleted) 13 { 14 if (!pListHead || !pToBeDeleted) 15 return; 16 17 if (pToBeDeleted->m_pNext != NULL) { 18 ListNode *pNext = pToBeDeleted->m_pNext; 19 pToBeDeleted->m_pNext = pNext->m_pNext; 20 pToBeDeleted->m_nKey = pNext->m_nKey; 21 22 delete pNext; 23 pNext = NULL; 24 } 25 else { //待删除节点为尾节点 26 ListNode *pTemp = pListHead; 27 while(pTemp->m_pNext != pToBeDeleted) 28 pTemp = pTemp->m_pNext; 29 pTemp->m_pNext = NULL; 30 31 delete pToBeDeleted; 32 pToBeDeleted = NULL; 33 } 34 }
标签:
原文地址:http://www.cnblogs.com/fengyiyangdeakaliusi/p/4648881.html