标签:c style class blog code java
方法一. 从链表的第一个结点开始遍历,顺序遍历到需删除结点的前一个结点,然后调整指针指向 T(n) = O(n)
1 void DeleteNode(ListNode** pListHead, ListNode* pToBeDeleted) 2 { 3 if(!pListHead || !pToBeDeleted) 4 return; 5 6 // 要删除的结点不是尾结点 7 if(pToBeDeleted->m_pNext != NULL) 8 { 9 ListNode* pNext = pToBeDeleted->m_pNext; 10 pToBeDeleted->m_nValue = pNext->m_nValue; 11 pToBeDeleted->m_pNext = pNext->m_pNext; 12 13 delete pNext; 14 pNext = NULL; 15 } 16 // 链表只有一个结点,删除头结点(也是尾结点) 17 else if(*pListHead == pToBeDeleted) 18 { 19 delete pToBeDeleted; 20 pToBeDeleted = NULL; 21 *pListHead = NULL; 22 } 23 // 链表中有多个结点,删除尾结点 24 else 25 { 26 ListNode* pNode = *pListHead; 27 while(pNode->m_pNext != pToBeDeleted) 28 { 29 pNode = pNode->m_pNext; 30 } 31 32 pNode->m_pNext = NULL; 33 delete pToBeDeleted; 34 pToBeDeleted = NULL; 35 } 36 }
剑指offer (13) O(1)时间删除单链表结点,布布扣,bubuko.com
标签:c style class blog code java
原文地址:http://www.cnblogs.com/wwwjieo0/p/3776518.html