Delete the linked list node at O (1) time.
Question: given a one-way linked list head pointer and a node pointer, define a function to delete the node at O (1) time.
The linked list nodes and functions are defined as follows:
struct ListNode{ int m_nValue; ListNode* m_pNext;};void DeleteNode(ListNode** pListHead,ListNode* pToBeDeleted);
Idea: We can easily get the next node of the node to be deleted. If we copy the content of the next node to the node to be deleted and overwrite the original content, delete the next node. This is equivalent to deleting the node to be deleted.
The implementation code is as follows:
1 # include <iostream> 2 # include <stdlib. h> 3 using namespace std; 4 5 // linked list structure 6 struct ListNode 7 {8 int m_nValue; 9 ListNode * m_pNext; 10 }; 11 12 // create a linked list node 13 ListNode * CreateListNode (int value) 14 {15 ListNode * pNode = new ListNode (); 16 pNode-> m_nValue = value; 17 pNode-> m_pNext = NULL; 18 return pNode; 19 20} 21 22 // All nodes in the traversal Link Table 23 void PrintList (ListNode * pHead) 24 {25 ListNode * pNode = pHead; 2 6 while (pNode! = NULL) 27 {28 cout <pNode-> m_nValue <"; 29 pNode = pNode-> m_pNext; 30} 31 cout <endl; 32} 33 // connect two nodes 34 void ConnectListNode (ListNode * pCurrent, ListNode * pNext) 35 {36 if (pCurrent = NULL) 37 {38 cout <"the previous node cannot be blank" <endl; 39 exit (1); 40} 41 else 42 {43 pCurrent-> m_pNext = pNext; 44} 45} 46 47 // Delete the linked list 48 void DestroyList (ListNode * pHead) 49 {50 ListNode * pNode = pHead; 51 while (pNode! = NULL) 52 {53 pHead = pHead-> m_pNext; 54 delete pNode; 55 pNode = pHead; 56} 57} 58 59 60 61 62 // consider four different situations 63 void DeleteNode (ListNode ** pListHead, ListNode * pTobeDeleted) 64 {65 if (! PListHead |! PTobeDeleted) 66 return; 67 68 // The node to be deleted is not the end node 69 if (pTobeDeleted-> m_pNext! = NULL) 70 {71 ListNode * pNext = pTobeDeleted-> m_pNext; 72 pTobeDeleted-> m_nValue = pNext-> m_nValue; 73 pTobeDeleted-> m_pNext = pNext-> m_pNext; 74 75 delete pNext; 76 pNext = NULL; 77} 78 // The linked list has only one node. delete the header node (also the end node) 79 else if (* pListHead = pTobeDeleted) 80 {81 delete pTobeDeleted; 82 pTobeDeleted = NULL; 83 * pListHead = NULL; 84} 85 // multiple nodes in the linked list are deleted from the End Node, you can only traverse the trace Table 86 else 87 {88 ListNode * pNode = * pList Head; 89 while (pNode-> m_pNext! = PTobeDeleted) 90 {91 pNode = pNode-> m_pNext; 92} 93 94 pNode-> m_pNext = NULL; 95 delete pTobeDeleted; 96 pTobeDeleted = NULL; 97} 98} 99 100 int main () 101 {102 // create node 103 ListNode * pNode1 = CreateListNode (1); 104 ListNode * pNode2 = CreateListNode (2 ); 105 ListNode * pNode3 = CreateListNode (3); 106 ListNode * pNode4 = CreateListNode (4); 107 ListNode * pNode5 = CreateListNode (5); 108 ListNode * pNode6 = CreateListNode (6 ); 109 ListNode * pNode7 = CreateListNode (7); 110 // connection node 111 ConnectListNode (pNode1, pNode2); 112 ConnectListNode (pNode2, pNode3); 113 ConnectListNode (pNode3, pNode4 ); 114 ConnectListNode (pNode4, pNode5); 115 ConnectListNode (pNode5, pNode6); 116 ConnectListNode (pNode6, pNode7); 117 118 PrintList (pNode1 ); 119 // delete node 120 DeleteNode (& pNode1, pNode4); 121 // print the linked list 122 PrintList (pNode1); 123 // Delete the linked list 124 DestroyList (pNode1 ); 125 126 return 0; 127}
The running result is as follows: