复杂链表的复制

复杂链表的结构

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

//其中random指向链表中的任意一个节点或者为空

要在o(n)时间内完成链表的复制,就要求每遍历一个节点就要能确定复制链表的random指针地址而实际上对于原始链表的每一个节点我们能确定对应的random指针地址。难点在于如何通过原始指针的random指针地址确定复制指针对应的random指针。本题思路:对链表中的每个节点进行复制,并将其插入到原始节点的后面,这样就能通过原始节点找到对应的复制节点,实现在0(1)的时间内找到复制节点的位置。

代码如下:

// ConsoleApplication13.cpp : 定义控制台应用程序的入口点。
/*复杂链表的复制
  
*/
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
struct RandomListNode {
int label;
struct RandomListNode *next, *random;
RandomListNode(int x) :
label(x), next(NULL), random(NULL) {
}
};
RandomListNode* CreateNode(int nValue)
{
RandomListNode* pNode = new RandomListNode(nValue);
return pNode;
}


void BuildNodes(RandomListNode* pNode, RandomListNode* pNext, RandomListNode* pSibling)
{
if (pNode != NULL)
{
pNode->next = pNext;
pNode->random = pSibling;
}
}


//复制链表
void CloneNode(RandomListNode* Phead){
RandomListNode* Pnode = Phead;
while (Pnode){
RandomListNode* clonenode=new RandomListNode(Pnode->label);
clonenode->next = Pnode->next;
Pnode->next = clonenode;
Pnode = clonenode->next;




}
}
//修改复制链表random指针
void ChangeRandomPointer(RandomListNode* phead){
RandomListNode *p, *q;
p = phead;
while (p){
q = p->random;
cout << p->label << endl;
if (q){
p->next->random = q->next;
}

p = p->next->next;

}
}
//拆分原始链表及复制表
RandomListNode* Solution(RandomListNode* phead){
RandomListNode *p, *q, *Clonehead;
p = phead;
Clonehead = p->next;
q = Clonehead;
while (p){
p->next = q->next;
if (p->next){
q->next = p->next->next;
}

p = p->next;
q = q->next;
}
return Clonehead;
}
RandomListNode* Clone(RandomListNode* phead){
CloneNode(phead);
ChangeRandomPointer(phead);
return Solution(phead);
}

//          -----------------
//         \|/              |
//  1-------2-------3-------4-------5
//  |       |      /|\             /|\
//  --------+--------               |
//          -------------------------
int _tmain(int argc, _TCHAR* argv[])
{
RandomListNode* pNode1 = CreateNode(1);
RandomListNode* pNode2 = CreateNode(2);
RandomListNode* pNode3 = CreateNode(3);
RandomListNode* pNode4 = CreateNode(4);
RandomListNode* pNode5 = CreateNode(5);


BuildNodes(pNode1, pNode2, pNode3);
BuildNodes(pNode2, pNode3, pNode5);
BuildNodes(pNode3, pNode4, NULL);
BuildNodes(pNode4, pNode5, pNode2);
RandomListNode* head= Clone(pNode1);
cout << head->random->next->label << endl;
system("pause");
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Unity,如果你想要复制一个复杂的数据结构,如链表(通常在脚本用自定义的Node类实现),你需要逐个节点地复制,并确保子节点和指针也被正确复制。这里是一个简化的步骤: 1. **定义节点类** (如果尚未存在): ```csharp public class Node<T> { public T data; public Node<T> next; // 其他可能的属性或方法... } ``` 2. **复制链表头结点**: 创建一个新的`Node`实例,将原始头结点的数据复制过去,然后设置新的头结点的`next`为复制后的`Node`。 ```csharp Node<T> newNode = new Node<T>(); newNode.data = originalHead.data; newNode.next = null; // 初始化新节点的next为null ``` 3. **递归复制链表**: 使用递归函数来遍历原链表,对于每个节点,复制其数据并更新新链表的下一个节点指向新复制的节点。 ```csharp public Node<T> CloneNode(Node<T> node, Node<T> clonedNode) { if (node != null) { clonedNode.data = node.data; clonedNode.next = CloneNode(node.next, clonedNode.next); // 递归处理下一个节点 } return clonedNode; } Node<T> clonedHead = CloneNode(originalHead, newNode); ``` 4. **处理复杂节点**: 如果链表的节点包含更复杂的对象,比如数组、列表等,你也需要递归地复制这些内容。 5. **检查和连接**: 完成复制后,确保新链表的最后一个节点的`next`指向原链表的最后一个节点,以保证连接完整。 ```csharp if (clonedHead.next == null) // 如果新链表不为空,连接最后两个节点 { clonedHead.next = originalHead.next; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值