# 剑指offer 25. 复杂链表的复制

## 原题

# -*- coding:utf-8 -*-
# class RandomListNode:
#     def __init__(self, x):
#         self.label = x
#         self.next = None
#         self.random = None
class Solution:
# 返回 RandomListNode
# write code here
while pNode:
pCloned = RandomListNode(pNode.label)
pCloned.next = pNode.next
pNode.next = pCloned
pNode = pCloned.next

while pNode:
pclone = pNode.next
if pNode.random:
pclone.random = pNode.random.next
pNode = pclone.next
pCloneNode = None
if pNode:
pNode.next = pCloneNode.next
pNode = pNode.next
while pNode:
pCloneNode.next = pNode.next
pCloneNode = pCloneNode.next
pNode.next = pCloneNode.next
pNode = pNode.next



C++ Version:

/*
struct RandomListNode {
int label;
struct RandomListNode *next, *random;
RandomListNode(int x) :
label(x), next(NULL), random(NULL) {
}
};
*/
class Solution {
public:
{
}

return;
}
RandomListNode* tempNode = NULL;
while (cloneNode != NULL){
tempNode = new RandomListNode(0);
tempNode->label = cloneNode->label;
tempNode->next = cloneNode->next;
tempNode->random = NULL;

cloneNode->next = tempNode;
cloneNode = tempNode->next;
}
}

//第二步，处理复杂指针的random
RandomListNode* tempNode = NULL;
while (cloneNode != NULL){
tempNode = cloneNode->next;

if (cloneNode->random != NULL){
tempNode->random = cloneNode->random->next;
}

cloneNode = tempNode->next;
}
}

//第三步，拆分复杂指针
RandomListNode* pClonedNode = NULL;

if(pNode != NULL){
pNode->next = pClonedNode->next;
pNode = pNode->next;
}

while(pNode != NULL){
pClonedNode->next = pNode->next;
pClonedNode = pClonedNode->next;
pNode->next = pClonedNode->next;
pNode = pNode->next;
}
}
};


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120