题解 | #JZ35 复杂链表的复制#
复杂链表的复制
https://www.nowcoder.com/practice/f836b2c43afc4b35ad6adc41ec941dba
/* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : label(x), next(NULL), random(NULL) { } }; */ class Solution { public: RandomListNode* Clone(RandomListNode* pHead) { RandomListNode* copyHead = NULL; RandomListNode* temp = pHead; RandomListNode* copytemp = copyHead; int index; vector <RandomListNode*> origin,copy,randomlist; if(pHead == NULL){ return NULL; } while(temp != NULL){ origin.push_back(temp); RandomListNode* node = new RandomListNode(temp->label); copy.push_back(node); randomlist.push_back(temp->random); // cout<< temp->label<<' '; if(copytemp == NULL){ copyHead = node; copytemp = copyHead; } else{ copytemp->next = node; copytemp=copytemp->next; } temp=temp->next; } copytemp = copyHead; for(int i = 0;i<origin.size();i++){ index = find(origin.begin(),origin.end(),randomlist[i])-origin.begin(); copytemp->random = copy[index]; copytemp = copytemp->next; } return copyHead; } };