题目:请实现函数ComplexListNode* Clone(ComplexListNode* pHead),复制一个复杂链表,在复杂
链表中,每个结点除了有一个m_pNext指针指向下一个结点外,还有一个m_pSibling指向链表中的任意
结点或者NULL。
结点的C++定义如下:
1 struct ComplexListNode 2 { 3 int m_nValue; 4 ComplexListNode* m_pNext; 5 ComplexListNode* m_pSibling; 6 }
本题以链表:
1 1->3->5->7->9
为例,
其中各个元素的Sibling如下:
第一个结点的sibling指向结点5
第二个结点的sibling指向结点9
第三个结点的sibling为NULL
第四个结点的sibling为第二个结点
第五个结点的sibling为NULL
此题目的解题步骤分为以下三步:
Step 1.将链表中每个结点复制一个链接在其后面
1 1->1->3->3->5->5->7->7->9->9
Step 2.解决链表的sibling问题
比如第一个结点的sibling为第三个结点,
那么可以遍历链表使
1 Node->next->sibling=Node->sibling
Step 3.此时将链表的偶数结点取出来即可,取出来即为复制的结点。
代码如下:
1 #include <iostream> 2 using namespace std; 3 4 struct ComplexListNode 5 { 6 int m_nValue; 7 ComplexListNode* m_pNext; 8 ComplexListNode* m_pSibling; 9 }; 10 11 ComplexListNode* CreateLinkList() 12 { 13 ComplexListNode* Head; 14 Head=new ComplexListNode(); 15 Head->m_nValue=1; 16 Head->m_pNext=NULL; 17 Head->m_pSibling=NULL; 18 19 ComplexListNode* Temp=Head; 20 21 ComplexListNode* Node2=new ComplexListNode(); 22 Node2->m_nValue=3; 23 Node2->m_pNext=NULL; 24 Node2->m_pSibling=NULL; 25 ComplexListNode* Node3=new ComplexListNode(); 26 Node3->m_nValue=5; 27 Node3->m_pNext=NULL; 28 Node3->m_pSibling=NULL; 29 ComplexListNode* Node4=new ComplexListNode(); 30 Node4->m_nValue=7; 31 Node4->m_pNext=NULL; 32 Node4->m_pSibling=NULL; 33 ComplexListNode* Node5=new ComplexListNode(); 34 Node5->m_nValue=9; 35 Node5->m_pNext=NULL; 36 Node5->m_pSibling=NULL; 37 38 Temp->m_pNext=Node2; 39 Temp=Temp->m_pNext; 40 Temp->m_pNext=Node3; 41 Temp=Temp->m_pNext; 42 Temp->m_pNext=Node4; 43 Temp=Temp->m_pNext; 44 Temp->m_pNext=Node5; 45 Temp=Temp->m_pNext; 46 47 ComplexListNode* TempNew1=Head; 48 TempNew1->m_pSibling=TempNew1->m_pNext->m_pNext; 49 50 ComplexListNode* TempNew2=Head; 51 TempNew2->m_pNext->m_pSibling=TempNew2->m_pNext->m_pNext->m_pNext->m_pNext; 52 53 ComplexListNode* TempNew3=Head; 54 TempNew3->m_pNext->m_pNext->m_pNext->m_pSibling=TempNew3->m_pNext; 55 56 ComplexListNode* TempNew4=Head; 57 TempNew4->m_pNext->m_pNext->m_pSibling=NULL; 58 59 ComplexListNode* TempNew5=Head; 60 TempNew4->m_pNext->m_pNext->m_pNext->m_pNext->m_pSibling=NULL; 61 62 return Head; 63 } 64 65 void PrintListLink(ComplexListNode* Head) 66 { 67 ComplexListNode *Temp=Head; 68 while(Temp!=NULL) 69 { 70 cout<<"Node Value: "<<Temp->m_nValue<<" "; 71 if(Temp->m_pSibling!=NULL) 72 cout<<"Sibling Value: "<<Temp->m_pSibling->m_nValue<<" "; 73 else 74 cout<<"Sibling Value:NULL"; 75 Temp=Temp->m_pNext; 76 cout<<endl; 77 } 78 } 79 80 ComplexListNode* StepOneCopyAndLink(ComplexListNode* Head) 81 { 82 ComplexListNode* Temp=Head; 83 84 while(Temp!=NULL) 85 { 86 ComplexListNode* NewNode=new ComplexListNode(); 87 NewNode->m_nValue=Temp->m_nValue; 88 NewNode->m_pNext=Temp->m_pNext; 89 Temp->m_pNext=NewNode; 90 Temp=NewNode->m_pNext; 91 } 92 93 return Head; 94 } 95 96 void StepTwoSiblingNode(ComplexListNode* Head) 97 { 98 ComplexListNode* Temp=Head; 99 while(Temp!=NULL) 100 { 101 if(Temp->m_pSibling!=NULL) 102 Temp->m_pNext->m_pSibling=Temp->m_pSibling; 103 104 Temp=Temp->m_pNext->m_pNext; 105 } 106 } 107 108 ComplexListNode* StepThreeDepartTheCopyLinkList(ComplexListNode* Head) 109 { 110 ComplexListNode* Head1=Head; 111 ComplexListNode* Head2=Head->m_pNext; 112 ComplexListNode* CopyHead=Head->m_pNext; 113 114 while(Head1->m_pNext->m_pNext!=NULL) 115 { 116 Head1->m_pNext=Head1->m_pNext->m_pNext; 117 Head1=Head1->m_pNext; 118 119 Head2->m_pNext=Head2->m_pNext->m_pNext; 120 Head2=Head2->m_pNext; 121 } 122 123 Head1->m_pNext=NULL; 124 125 return CopyHead; 126 } 127 128 int main(void) 129 { 130 ComplexListNode* Head; 131 Head=CreateLinkList(); 132 cout<<"Original LinkList:"<<endl; 133 PrintListLink(Head); 134 ComplexListNode* NewHead; 135 NewHead=StepOneCopyAndLink(Head); 136 cout<<"Step1:Copy Node After Each Node"<<endl; 137 PrintListLink(NewHead); 138 StepTwoSiblingNode(NewHead); 139 cout<<"Step2:Copy Sibling Of Node"<<endl; 140 PrintListLink(NewHead); 141 cout<<"Step3:"<<endl; 142 ComplexListNode* CopyHead; 143 CopyHead=StepThreeDepartTheCopyLinkList(NewHead); 144 cout<<"The Old LinkList is:"<<endl; 145 PrintListLink(NewHead); 146 cout<<"The Copy LinkList is:"<<endl; 147 PrintListLink(CopyHead); 148 system("pause"); 149 return 0; 150 }
运行截图: