剑指offer第十三题:在O(1)时间内删除链表结点
1 //============================================================================ 2 // Name : JZ-C-13.cpp 3 // Author : Laughing_Lz 4 // Version : 5 // Copyright : All Right Reserved 6 // Description : 在O(1)时间内删除链表结点 7 //============================================================================ 8 9 #include <iostream> 10 #include "List.h" 11 using namespace std; 12 13 /*typedef struct ListNode { 14 int value; 15 ListNode* next; 16 } NODE;*/ 17 18 void DeleteNode(ListNode** HeadNode, ListNode* ToBeDeleted) { 19 if (!HeadNode || !ToBeDeleted) { 20 return; 21 } 22 if (ToBeDeleted->m_pNext != NULL) { // 要删除的结点不是尾结点 23 ListNode* p = ToBeDeleted->m_pNext; 24 ToBeDeleted->m_nValue = p->m_nValue; 25 ToBeDeleted->m_pNext = p->m_pNext; 26 delete p; 27 p = NULL; 28 } else if (*HeadNode == ToBeDeleted) { // 链表只有一个结点,删除头结点(也是尾结点) 29 delete ToBeDeleted; 30 ToBeDeleted = NULL; 31 *HeadNode = NULL; 32 } else { // 链表中有多个结点,删除尾结点 33 ListNode* p = *HeadNode; 34 while (p->m_pNext != ToBeDeleted) { 35 p = p->m_pNext; 36 } 37 p->m_pNext = NULL; 38 delete ToBeDeleted; 39 ToBeDeleted = NULL; 40 } 41 42 } 43 // ====================测试代码==================== 44 void Test(ListNode* pListHead, ListNode* pNode) { 45 // printf("The original list is: \n"); 46 cout << "The original list is:" << endl; 47 PrintList(pListHead); 48 49 // printf("The node to be deleted is: \n"); 50 cout << "The node to be deleted is:" << endl; 51 PrintListNode(pNode); 52 53 DeleteNode(&pListHead, pNode); 54 // printf("The result list is: \n"); 55 cout << "The result list is:" << endl; 56 PrintList(pListHead); 57 } 58 59 // 链表中有多个结点,删除中间的结点 60 void Test1() { 61 ListNode* pNode1 = CreateListNode(1); 62 ListNode* pNode2 = CreateListNode(2); 63 ListNode* pNode3 = CreateListNode(3); 64 ListNode* pNode4 = CreateListNode(4); 65 ListNode* pNode5 = CreateListNode(5); 66 67 ConnectListNodes(pNode1, pNode2); 68 ConnectListNodes(pNode2, pNode3); 69 ConnectListNodes(pNode3, pNode4); 70 ConnectListNodes(pNode4, pNode5); 71 72 Test(pNode1, pNode3); 73 74 DestroyList(pNode1); 75 } 76 77 // 链表中有多个结点,删除尾结点 78 void Test2() { 79 ListNode* pNode1 = CreateListNode(1); 80 ListNode* pNode2 = CreateListNode(2); 81 ListNode* pNode3 = CreateListNode(3); 82 ListNode* pNode4 = CreateListNode(4); 83 ListNode* pNode5 = CreateListNode(5); 84 85 ConnectListNodes(pNode1, pNode2); 86 ConnectListNodes(pNode2, pNode3); 87 ConnectListNodes(pNode3, pNode4); 88 ConnectListNodes(pNode4, pNode5); 89 90 Test(pNode1, pNode5); 91 92 DestroyList(pNode1); 93 } 94 95 // 链表中有多个结点,删除头结点 96 void Test3() { 97 ListNode* pNode1 = CreateListNode(1); 98 ListNode* pNode2 = CreateListNode(2); 99 ListNode* pNode3 = CreateListNode(3); 100 ListNode* pNode4 = CreateListNode(4); 101 ListNode* pNode5 = CreateListNode(5); 102 103 ConnectListNodes(pNode1, pNode2); 104 ConnectListNodes(pNode2, pNode3); 105 ConnectListNodes(pNode3, pNode4); 106 ConnectListNodes(pNode4, pNode5); 107 108 Test(pNode1, pNode1); 109 110 DestroyList(pNode1); 111 } 112 113 // 链表中只有一个结点,删除头结点 114 void Test4() { 115 ListNode* pNode1 = CreateListNode(1); 116 117 Test(pNode1, pNode1); 118 } 119 120 // 链表为空 121 void Test5() { 122 Test(NULL, NULL); 123 } 124 125 int main(int argc, char** argv) { 126 Test1(); 127 Test2(); 128 Test3(); 129 Test4(); 130 Test5(); 131 132 return 0; 133 }
时间: 2024-12-24 07:05:37