LeetCode || Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
思路1:最傻瓜的方法是首先遍历一次建立next关系的新list。然后第二次遍历处理random关系,对于每个有random结点的node,我们都从表头開始遍历寻找其random的结点。然后给新list的相应结点赋值。这种话对于每个node,寻找random须要花费O(N)时间。故总时间复杂度为O(N^2)。这个方案会超时。
思路2:改进思路1。假设处理random关系的复制,使其复杂度降为O(N)?答案是要找到原node的random指向的结点在新list中相应的那个结点,假设能一下找到,那么就攻克了;实现方法是使用一个map<old, new>。记录原node与新node的相应关系。然后进行两次遍历,第一次建立next关系的新list。第二次给新list建立random指向关系;代码例如以下:
/*** 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) {map<RandomListNode *, RandomListNode *> mapNodes;RandomListNode *newList = NULL;RandomListNode *newHead = NULL;RandomListNode *p = head;if(head==NULL)return NULL;while(p!=NULL){RandomListNode *q = new RandomListNode(p->label);mapNodes[p] = q;if(newHead==NULL){newHead = q;newList = q; }else{newList->next = q;newList = newList->next;}p = p->next; }p = head;newList = newHead;while(p!=NULL){if(p->random!=NULL)newList->random = mapNodes[p->random]; //注意这里要用p->random而不是pp = p->next;newList = newList->next;}return newHead; //返回值是newHead而不是newList,由于此时newList指向尾部}
};
思路3:思路2没有改变原list结构,可是使用了map,故须要额外的内存空间,假设原list解构可变。那么能够不必使用map记录映射关系,而是直接把复制的node放在原node的后面,这样结构变为:
上面为第一次遍历,第二次遍历时把红色的新node的random域赋值。规则是:
newNode->ranodm = oldNode->random->next;
然后第三次遍历把上面的链表拆分为两个就可以。代码例如以下:
/*** 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) {RandomListNode *newList = NULL;RandomListNode *newHead = NULL;RandomListNode *p = head;if(head==NULL)return NULL;while(p!=NULL){RandomListNode *q = new RandomListNode(p->label);q->next = p->next;p->next = q;p = q->next;}p = head;while(p!=NULL){if(p->random != NULL)p->next->random = p->random->next;p = p->next->next;}p = head;while(p!=NULL && p->next!=NULL){ //注意这里的条件,首先要推断p是否为nullif(p==head){newHead = p->next;newList = newHead;}else{newList->next = p->next;newList = newList->next;}p->next = newList->next;p = p->next;}return newHead; //返回值是newHead而不是newList,由于此时newList指向尾部}
};
參考链接:http://www.2cto.com/kf/201310/253477.html
转载于:https://www.cnblogs.com/ldxsuanfa/p/10801565.html
LeetCode || Copy List with Random Pointer相关推荐
- LeetCode Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [Leetcode] Copy list with random pointer 对带有任意指针的链表深度拷贝
A linked list is given such that each node contains an additional random pointer which could point t ...
- LeetCode 138. Copy List with Random Pointer
LeetCode 138. Copy List with Random Pointer 参考链接:http://www.cnblogs.com/grandyang/p/4261431.html Sol ...
- leetcode -day8 Copy List with Random Pointer Single Number I II
五一中间断了几天,开始继续... 1. Copy List with Random Pointer A linked list is given such that each node cont ...
- 138. Copy List with Random Pointer
/** 138. Copy List with Random Pointer * 2016-5-22 by Mingyang* 要遍历两次,第一次用来找到所有的next节点,并且把新旧节点全部存在ha ...
- LeetCode OJ - Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 【LeetCode】Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- Leetcode 138 Copy List With Random Pointer
题目描述 A linked list is given such that each node contains an additional random pointer which could po ...
- Copy List with Random Pointer
https://leetcode.com/problems/copy-list-with-random-pointer/ A linked list is given such that each n ...
最新文章
- 嵌入式linux支持python,【python】嵌入式设备上python的使用
- 【剑指offer】反转链表,C++实现(链表)
- 推荐算法炼丹笔记:排序模型CTR点击率预估系列
- zip: stdin: unexpected end of file tar: 归档文件中异常的 EOF tar: 归档文件中异常的 EOF tar: Error is not recoverable
- 源路由 小实验 小发现
- Github上影响力最大的十位Pythoner
- matplotlib.pyplot 中文乱码问题解决
- 5大主流主链排行版出炉; 以太坊新生合约数持续3周下跌; 各大榜单均现“黑马”冲榜!| 数据周榜...
- sysdate取年取月取日
- 我设想的可扩展结构(插件) (二)
- memcached安装部署及启动错误排查
- 孙正义宣布回购55亿美元股票,软银股价创10年最大单日涨幅...
- Flutter插件shared_preferences数据存储的使用
- JES专栏:Portlets的国际化和本地化(eNews 第二十八期/2007.09)
- 中国经济衰退 周期投资风向标
- android usb ftdi,android-Nexus7 USB主机FTDI设备未检测到
- PaddleOCR .NET 识别图片中文字
- Java两个日期相差的天数计算
- Android和win10的融合,微软Windows10更新版亮相 更好融合iOS、Android系统
- pip速度慢,更换清华镜像源试试?
热门文章
- wxpython实现界面跳转
- 一张有趣的图--《teach yourself c++ in 21 days》
- 广州中山大道BRT不开“巨无霸”公交车
- 使用Spring Task完成定时任务
- 北京修复宕机故障之旅
- 为什么修改了ie级别里的activex控件为启用后,还是无法下载,显示还是ie级别设置太高?
- python-windows安装相关问题
- (原創) 如何使用C++/CLI读/写jpg檔? (.NET) (C++/CLI) (GDI+) (C/C++) (Image Processing)
- linux 安装maven
- “陪护机器人”研报:距离真正“陪护”还差那么一点