【刷题之路】复杂链表的复制
2016-07-18 14:55
246 查看
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head
三步走:
第一步,复制链表节点A->B->C 变为 A->A'->B->B'->C->C' ,目的为了第二步
第二步,重建random指针位置,此时,复制后的指针指向的位置应该就是原节点random指针指向节点的next,这样就重建了复杂链表
第三步,将复制出的节点分离并组合成为一个新的链表,这就是我们复制出的链表
代码如下:
/*
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;
RandomListNode* copyhead;
RandomListNode* rebuild;
RandomListNode* res;
copyhead=Copy(pHead);
rebuild=Rbuild(copyhead);
res=Divide(rebuild);
return res;
}
RandomListNode* Copy(RandomListNode* pHead){
RandomListNode* temp=pHead;
RandomListNode* next;
while(temp){
RandomListNode *node = new RandomListNode(temp->label);
next=temp->next;
temp->next=node;
node->next=next;
temp=next;
}
return pHead;
}
RandomListNode* Rbuild(RandomListNode* pHead){
RandomListNode* temp=pHead;
while(temp){
if(temp->random) temp->next->random=temp->random->next;
temp=temp->next->next;
}
return pHead;
}
RandomListNode* Divide(RandomListNode* pHead){
RandomListNode* temp=pHead->next->next;
RandomListNode* newhead=pHead->next;
RandomListNode* newtemp=newhead;
while(temp){
newtemp->next=temp->next;
newtemp=newtemp->next;
temp=temp->next->next;
}
return newhead;
}
};
三步走:
第一步,复制链表节点A->B->C 变为 A->A'->B->B'->C->C' ,目的为了第二步
第二步,重建random指针位置,此时,复制后的指针指向的位置应该就是原节点random指针指向节点的next,这样就重建了复杂链表
第三步,将复制出的节点分离并组合成为一个新的链表,这就是我们复制出的链表
代码如下:
/*
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;
RandomListNode* copyhead;
RandomListNode* rebuild;
RandomListNode* res;
copyhead=Copy(pHead);
rebuild=Rbuild(copyhead);
res=Divide(rebuild);
return res;
}
RandomListNode* Copy(RandomListNode* pHead){
RandomListNode* temp=pHead;
RandomListNode* next;
while(temp){
RandomListNode *node = new RandomListNode(temp->label);
next=temp->next;
temp->next=node;
node->next=next;
temp=next;
}
return pHead;
}
RandomListNode* Rbuild(RandomListNode* pHead){
RandomListNode* temp=pHead;
while(temp){
if(temp->random) temp->next->random=temp->random->next;
temp=temp->next->next;
}
return pHead;
}
RandomListNode* Divide(RandomListNode* pHead){
RandomListNode* temp=pHead->next->next;
RandomListNode* newhead=pHead->next;
RandomListNode* newtemp=newhead;
while(temp){
newtemp->next=temp->next;
newtemp=newtemp->next;
temp=temp->next->next;
}
return newhead;
}
};
相关文章推荐
- [C/C++]反转链表
- C#实现基于链表的内存记事本实例
- C#模拟链表数据结构的实例解析
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- C++利用静态成员或类模板构建链表的方法讲解
- C++实现简单的学生管理系统
- Linux内核链表实现过程
- C++链表倒序实现方法
- C#通过链表实现队列的方法
- Node.js环境下JavaScript实现单链表与双链表结构
- C#实现的简单链表类实例
- 找出链表倒数第n个节点元素的二个方法
- Java数据结构之简单链表的定义与实现方法示例
- Java模拟有序链表数据结构的示例
- C语言单循环链表的表示与实现实例详解
- C++实现的链表类实例
- PHP小教程之实现链表
- PHP中模拟链表和链表的基本操作示例
- C语言双向链表的表示与实现实例详解
- js链表操作(实例讲解)