1.题目
剑指 Offer 35. 复杂链表的复制
请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。
示例 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。
提示:
-10000 <= Node.val <= 10000
Node.random 为空(null)或指向链表中的节点。
节点数目不超过 1000 。
2.自我思路及实现
未实现
next指针可以完成,random指针不知道如何实现,尝试通过一个哈希表来实现,但无法定位到新节点
/*
// 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) {
Node res = null;
if(head == null)
return res;
Map<Integer, Node> map = HashMap<>();
Node node = head;
int index;
//next指针
while(head != null)
{
Node cur = new Node(head.val);
map.put(index, cur);
head = head.next;
cur.next = head;
index ++;
}
//random指针:???
index = 0;
while(node != null)
{
Node cur = map.get(index);
if(node.random != null)
cur.random = node.random;
}
}
}
3.总结思路及实现
1.哈希表
使用哈希表存储(旧节点, 复制的新节点),先遍历一遍旧节点从而创建对应新节点,再遍历一次旧节点来将新节点的对应关系建立起来
时间:N, 两次遍历
空间: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 temp = head;
//创建对应新节点
while(temp != null)
{
map.put(temp, new Node(temp.val));
temp = temp.next;
}
//建立对应关系
temp = head; //用于找到新节点的头
while(head != null)
{
Node cur = map.get(head);
cur.next = map.get(head.next);
cur.random = map.get(head.random);
head = head.next;
}
return map.get(temp);
}
}
2.模拟哈希表,新旧节点交替
A的复制节点为A’,将A’插入到A后,形成A-A’-B- B’,再处理random指针
最后将两个链表拆分
时间:N,三次遍历
空间:1,没有使用额外空间
/*
// 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;
Node temp = head;
//将新节点插入链表
while(temp != null)
{
Node cur = new Node(temp.val);
cur.next = temp.next;
temp.next = cur;
temp = cur.next;
}
//random指针
temp = head;
while(temp != null)
{
if(temp.random == null)
temp.next.random = null;
else
temp.next.random = temp.random.next;
temp = temp.next.next;
}
//拆分两个链表
temp = head.next;
Node newHead = temp;
while(temp.next != null)
{
head.next = temp.next;
temp.next = temp.next.next;
temp = temp.next;
head = head.next;
}
head.next = null;//处理旧链表的尾部
return newHead;
}
}
3.dfs
可将该链表看作图,故可使用图的dfs遍历
使用哈希表存储已被标记过的节点和其对应的新节点
递归方法:遍历节点并复制
终止条件:节点为空
递归函数:节点已被访问,返回该节点即可;节点未被访问,创建新的节点并存入哈希表,新节点的next和random指针为旧节点的next和random的复制
时间:N
空间:N
class Solution {
Map<Node, Node> visited = new HashMap<>();
public Node copyRandomList(Node head) {
if(head == null)
return null;
if(visited.containsKey(head))
return visited.get(head);
Node newNode = new Node(head.val);
visited.put(head, newNode);
newNode.next = copyRandomList(head.next);
newNode.random = copyRandomList(head.random);
return newNode;
}
}
4.bfs
使用哈希表存储已访问过的节点及其对应复制节点
使用队列存储邻接节点
时间:N
空间:N
class Solution {
public Node copyRandomList(Node head) {
if(head == null)
return null;
Queue<Node> queue = new LinkedList<>();
Map<Node, Node> visited = new HashMap<>();
Node newHead = new Node(head.val);
visited.put(head, newHead);
queue.offer(head);
while( !queue.isEmpty())
{
Node temp = queue.poll();
if( temp.next != null && !visited.containsKey(temp.next))
{
queue.offer(temp.next);
visited.put(temp.next, new Node(temp.next.val));
}
if( temp.random != null && !visited.containsKey(temp.random))
{
queue.offer(temp.random);
visited.put(temp.random, new Node(temp.random.val));
}
visited.get(temp).next = visited.get(temp.next);
visited.get(temp).random = visited.get(temp.random);
}
return newHead;
}
}
4.相关知识
- 有DFS 就有BFS
- 哈希表的对应关系