[LeetCode] Copy List with Random Pointer

来源:互联网 发布:实体店铺设计软件 编辑:程序博客网 时间:2024/05/22 17:40
RandomListNode *copyRandomList(RandomListNode *head) {if(head == NULL) {return NULL;}RandomListNode* curNode = head;    while(curNode != NULL) {RandomListNode* curCopyNode = new RandomListNode(curNode->label);RandomListNode* nextNode = curNode->next;curNode->next = curCopyNode;curCopyNode->next = nextNode;curNode = nextNode;}curNode = head;while(curNode != NULL) {if(curNode->random != NULL) {curNode->next->random = curNode->random->next;}curNode = curNode->next->next;}RandomListNode* copyListHead = head->next;curNode = head;while(curNode != NULL) {RandomListNode* curCopyNode = curNode->next;RandomListNode* nextNode = curCopyNode->next;RandomListNode* nextCopyNode = NULL;if(nextNode != NULL) {nextCopyNode = nextNode->next;}curNode->next = nextNode;curCopyNode->next = nextCopyNode;curNode = nextNode;}return copyListHead;}

0 0