剑指offer系列19:复杂链表的复制
作者:互联网
这道题是典型的分治法,将一个大问题分解成几小步解决。一定要注意在使用指针的时候指针指向是否为空的问题。在指针指向时,可以将一个指向为空的指针作为赋值来写,但是空指针不能指向任何地方(指向空也不行),这点一定要记住。
#include<iostream> #include<vector> using namespace std; struct RandomListNode { int label; struct RandomListNode *next, *random; /* RandomListNode(int x) : label(x), next(NULL), random(NULL) { } */ }; class Solution { public: RandomListNode* Clone(RandomListNode* pHead) { if (pHead == NULL) return NULL; //3步 //第一步,将复杂链表复制成double nodeclone(pHead); connect(pHead); return reconnect(pHead); } void nodeclone(RandomListNode* Head) { RandomListNode*p = Head; while (p != NULL) { //插入节点 //cur = p->next; RandomListNode *curnode = new RandomListNode(); curnode->label = p->label; curnode->next = p->next; curnode->random = NULL;//这一句必须要写? p->next = curnode; p = curnode->next;//悬浮指针? } } void connect(RandomListNode* Head) { //第二步:将复制出的结点的随机指针指向该有的位置 RandomListNode*p = Head; while (p != NULL)//在指针访问的时候,时刻注意是否为空 { RandomListNode *curnode = p->next; if (p->random != NULL) { curnode->random = p->random->next; } p = curnode->next; } } RandomListNode *reconnect(RandomListNode* Head) { //第三步:将链表拆为两部分 RandomListNode*p = Head; RandomListNode *copy = p->next; while (p!= NULL) { RandomListNode *curnode = p->next; if (curnode != NULL) { p->next = curnode->next; } p = curnode; } return copy; } }; int main() { RandomListNode list[5]; list[0].label = 1; list[0].next = &list[1]; list[0].random = &list[2]; list[1].label = 2; list[1].next = &list[2]; list[1].random = &list[4]; list[2].label = 3; list[2].next = &list[3]; list[2].random = NULL; list[3].label = 4; list[3].next = &list[4]; list[3].random = &list[1]; list[4].label = 5; list[4].next =NULL; list[4].random = NULL; Solution solu; RandomListNode *re = solu.Clone(list); int count=0; while (re != NULL) { //cout << re->label << " "<<re->random->label<<","; cout << re->label<<" "; if (re->random != NULL) cout << re->random->label; cout << endl; count++; re = re->next; } //cout << endl; cout << "number of array:"<<count << endl; return 0; }
标签:NULL,offer,19,random,list,next,链表,RandomListNode,curnode 来源: https://www.cnblogs.com/neverland0718/p/16210290.html