LC-拷贝有随机指针的链表

拷贝有随机指针的链表

//拷贝有随机指针的链表
/*
现在有一个这样的链表:链表的每一个节点都附加了一个随机指针,
随机指针可能指向链表中的任意一个节点或者指向空。
请对这个链表进行深拷贝。
*/
//Definition for singly-linked list with a random pointer.
struct RandomListNode {
     int label;
     RandomListNode *next, *random;
     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
};

class Solution {
public:
	//采用申请节点并复制键值->插入节点->复制随机指针->拆分链表的方式
	//复制节点后插入可以方便复制随机指针:找到原链表节点的随机指针位置,下一位置即为
	//拷贝的随机指针位置。
	RandomListNode *copyRandomList(RandomListNode *head) {
		if (head == nullptr)
			return nullptr;
		RandomListNode* pNode;
		RandomListNode* cur = head;
		//复制节点并插入链表中
		while (cur != nullptr)
		{
			pNode = new RandomListNode(cur->label);
			RandomListNode* nextNode = cur->next;
			pNode->next = cur->next;
			cur->next = pNode;
			cur = nextNode;
		}
		//找到随机节点位置并复制
		cur = head;
		while (cur != nullptr)
		{
			pNode = cur->next;
			if (cur->random)
			{
				pNode->random = cur->random->next;
			}
			else
			{
				pNode->random = nullptr;
			}
			cur = pNode->next;
		}
		//拆分链表
		cur = head;
		RandomListNode* newHead = cur->next;
		while(cur!=nullptr)
		{
			pNode = cur->next;
			cur = cur->next = pNode->next;
			
		}
		return newHead;
	}
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值