复杂链表的复制
一、题目描述
请实现 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 。
作者:Krahets
链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9p0yy1/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
二、解题思路及代码实现
1、解题思路
我的实现方法是:借助map和vector容器,第一遍循环构造新链表,第二遍循环处理random指针。之所以使用map,是因为它的查找效率非常高,使用vector是其随机访问速度快。我的实现运行速度快,缺点是消耗内存较多,不如官方的方法节约内存。
2、C++代码实现
/*
// 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) {
Node* ret = NULL;
Node* current = NULL;
map<Node*, int> index_map_old;
vector<Node*> new_nodes;
Node* tmp_old = head;
Node* tmp_new = ret;
int index = 0;
new_nodes.reserve(50);
//构建新链表、保存旧链表节点索引、保存新节点指针
while (tmp_old != NULL) {
Node* tmp = new Node(tmp_old->val);
tmp->next = NULL;
if (ret == NULL) {
ret = tmp;
current = tmp;
}
if (current != tmp) {
current->next = tmp;
current = tmp;
}
index_map_old[tmp_old] = index++;
new_nodes.push_back(tmp);
tmp_old = tmp_old->next;
}
//实现random指针赋值
tmp_old = head;
tmp_new = ret;
while (tmp_old != NULL) {
if (tmp_old->random == NULL) {
tmp_new->random = NULL;
} else {
int index_tmp = index_map_old[tmp_old->random];
tmp_new->random = new_nodes[index_tmp];
}
tmp_old = tmp_old->next;
tmp_new = tmp_new->next;
}
return ret;
}
};
三、提交结果
总结
从提交结果看,效率确实是挺高的,但内存使用较多,想了一下,又没有发现优化方案,查看官方解题思路,确实是又快又省内存。观摩一下官方解决方案:
class Solution {
public:
Node* copyRandomList(Node* head) {
if(head == nullptr) return nullptr;
Node* cur = head;
unordered_map<Node*, Node*> map;
// 3. 复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射
while(cur != nullptr) {
map[cur] = new Node(cur->val);
cur = cur->next;
}
cur = head;
// 4. 构建新链表的 next 和 random 指向
while(cur != nullptr) {
map[cur]->next = map[cur->next];
map[cur]->random = map[cur->random];
cur = cur->next;
}
// 5. 返回新链表的头节点
return map[head];
}
};
作者:Krahets
链接:https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/9plk45/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。