# 用O(1)的时间复杂度删除单链表中的某个节点

```struct ListNode
{
int        m_nKey;
ListNode*  m_pNext;
};```

`void DeleteNode(ListNode* pListHead, ListNode* pToBeDeleted);`

``` 1 /* Delete a node in a list with O(1)
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 {
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 { //待删除节点为尾节点
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 }```

原文作者：CloudDeveloper
原文地址: https://www.cnblogs.com/bakari/p/4013812.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。