11.复制带随机指针的链表

题目描述

给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。

构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。

例如,如果原链表中有 X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random --> y 。

返回复制链表的头节点。

用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:

val:一个表示 Node.val 的整数。
random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为  null 。
你的代码 只 接受原链表的头节点 head 作为传入参数。

题目链接

https://leetcode-cn.com/problems/copy-list-with-random-pointer/

样例描述

示例1:

输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]

示例2:

输入:head = [[1,1],[2,1]]
输出:[[1,1],[2,1]]

示例3:

输入:head = [[3,null],[3,0],[3,null]]
输出:[[3,null],[3,0],[3,null]]

示例4:

输入:head = []
输出:[]
解释:给定的链表为空(空指针),因此返回 null。

题目说明

  1. 深拷贝,将原来链表的val值,next值,random值完全拷贝
  2. 保证拷贝后的链表和原来的链表不重叠
  3. 原来的链表必须保证原样

思路

暴力:
将val, next, random依次拷贝
发现无法拷贝random
参考资料后,介绍两种方法解决问题。

解法一介绍

思路:

第一步:添加指针,每个新创建的节点是在原节点后面

第二步:设置新链表的随机指针

第三步:分离链表

时间复杂度

O(n)

代码

/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
*/
class Solution {
public:
Node* copyRandomList(Node* head) {
if (!head) {
return head;
}
Node* front = head;
Node* behind = front->next;
// 将复制的结点插到每个结点的后面,包括value
while (front) {
Node* n = new Node(NULL, NULL, NULL);
n->val = front->val;
n->next = front->next;
front->next = n;
front = n->next;
}
// 定义要返回的结点
Node* new_head = head->next;
// 给复制的random赋值
front = head;
behind = front->next;
while (behind) {
if (front->random) {
behind->random = front->random->next;
}
if (front->next->next && behind->next->next) {
front = front->next->next;
behind = behind->next->next;
}
if (!behind->next) {
if (front->random) {
behind->random = front->random->next;
}
break;
}
}
// 给复制的next赋值
front = head;
behind = front->next;
while (behind->next) {
front->next = behind->next;
front = front->next;
behind->next = front->next;
behind = behind->next;
}
front->next = NULL;
behind->next = NULL;
return new_head;
}
};

解法2介绍

思路

第一步:创建哈希表,将原来的结点和要克隆的结点(同时克隆val)放入

第二步:克隆next和random

时间复杂度

O(n)

代码

/*
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
*/
class Solution {
public Node copyRandomList(Node head) {
if (head == null) {
return null;
}
Map<Node, Node> map = new HashMap<Node, Node>();
Node p = head;
// 将所有的结点放入map中
while (p != null) {
Node newNode = new Node(p.val);
map.put(p, newNode);
p = p.next;
}
p = head;
// 设置新的next和random
while (p != null) {
// 设置新的next值
map.get(p).next = map.get(p.next);
// 设置新的random值
if (p.random != null) {
map.get(p).random = map.get(p.random);
}
p = p.next;
}
return map.get(head);
}
}

遇到的问题

问题1:Random pointer of node with label 7 points to a node from the original list.
原因:拷贝random错误

问题2:Next pointer of node with label 7 from the original list was modified.
原因:原来的链表再拷贝时被改变

posted @   jsqup  阅读(360)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示