Copy List with Random Pointer(复杂链表复制)
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
第一种方法:使用map存放原节点和其复制节点。然后再给复制节点的next/random指针初始化。初始化时,原节点p,p的复制节点的next=p.next的复制节点
见代码:
public RandomListNode copyRandomList(RandomListNode head) {
if(head==null) return null;
//map存放原节点和复制节点
Map<RandomListNode,RandomListNode> map=new HashMap<RandomListNode,RandomListNode>();
RandomListNode p=head;
//将所有节点及其复制节点放到hashmap中
while(p!=null){
map.put(p,new RandomListNode(p.label));
p=p.next;
}
//给复制节点的指针赋值
p=head;
while(p!=null){
map.get(p).next=map.get(p.next);//p的复制节点的next等于p.next的复制节点。
map.get(p).random=map.get(p.random);
p=p.next;
}
return map.get(head);
}
第二种方法:
分三步:
1、根据next指针,复制原链表,在原链表中复制,将所有节点复制到原节点后面(如a->b->c复制成a->a'->b->b'->c->c')
2、根据链表的random指针,在新链表中初始化复制节点的random指针。.next.random=.random.next
3、从复制后的链表中拆分出新的复制链表返回
如果只有一个next指针,则省略上面第2步
public RandomListNode copyRandomList(RandomListNode head) {
if(head==null) return null;
RandomListNode pCur=head;//用于遍历
//第一步,根据next指针复制如a->b->c复制成a->a'->b->b'->c->c'
while(pCur!=null){
RandomListNode node=new RandomListNode(pCur.label);
node.next=pCur.next;
pCur.next=node;
pCur=node.next;
}
pCur=head;
//第二步,初始化复制后节点的random指针
while(pCur!=null){
if(pCur.random!=null){ //不用判断p.next是否为空,因为p不为空,p.next是复制节点,肯定不为空
pCur.next.random=pCur.random.next;//要指向p.random的复制节点,所以有.next
}
pCur=pCur.next.next;
}
//第三步,拆分
RandomListNode phead=head.next;
RandomListNode p=phead;
pCur=head;
while(pCur!=null){
pCur.next=pCur.next.next;
if(p.next!=null){
p.next=p.next.next;
}
p=p.next;
pCur=pCur.next;
}
return phead;
}
可以看出,上面第一种方法简单很多。
Copy List with Random Pointer(复杂链表复制)的更多相关文章
- leetcode 138. Copy List with Random Pointer复杂链表的复制
python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...
- 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表
133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- [LeetCode]Copy List with Random Pointer &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- LeetCode OJ:Copy List with Random Pointer(复制存在随机链接的链表)
A linked list is given such that each node contains an additional random pointer which could point t ...
随机推荐
- web中间件切换(was切tomcat)
一.数据源迁移: ①数据源配置在web容器还是在项目本身? 根据开发与生产分离原则选择配置到web容器,以免开发泄露数据库密码. ②数据库密码加密 原先was的数据源直接在console控制,密码是密 ...
- linq---我为你提笔序,你的美不只查询语句
LinQ百度百科对她这样解释,是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以查询数据库相同的方式操作内存数据. LINQ是Language Int ...
- Dynamics CRM2013 注释中的内容无法正常显示问题
CRM2013中在表单中插入注释,并把注释设置成默认选项卡后 打开一个已经挂了附件的表单,但却显示找不到记录 必须要再点击下注释,内容才会出来 查了半天不得其解,终于在ur1 for CRM2013 ...
- Android项目-高考作文-使用ORMLite抽象公共的Dao层
1, 定义统一的Dao接口 public interface IDao<T> { public abstract T getSingleById(int id); public abstr ...
- SpriteBuilder中使用Node类型的ccb动画节点删除时崩溃的问题
因为节点需要呈现动画效果,虽然只有两个不同帧. 在SpriteBuilder中新建Bullet.ccb文件,类型为node. 添加如上2张图片,并制作动画效果帧. 在游戏中子弹遇到障碍物会被删除,时机 ...
- ROS_Kinetic_05 ROS基础内容(二)
ROS_Kinetic_05 ROS基础内容(二) 1. ROS节点node 官网教程:http://wiki.ros.org/cn/ROS/Tutorials/UnderstandingNodes ...
- JAVA之旅(九)——Object类,equals,toString,getClass,内部类访问规则,静态内部类,内部类原则,匿名内部类
JAVA之旅(九)--Object类,equals,toString,getClass,内部类访问规则,静态内部类,内部类原则,匿名内部类 天天被一些琐事骚扰,学习还得继续 一.Object类 Obj ...
- C++ Primer 有感(复制控制)
1.不管类是否定义了自己的析构函数,编译器都 自动执行类中非static数据成员的析构函数. 2.如果我们没有定义复制构造函数,编译器就会为我们合成一个.合成复制构造函数的行为是,执行逐个成员初始化, ...
- CTBS问题百科
1.浏览CTBS网站时,Service Unavailable或应用程序池自动停止的现象 解决方法: 点击"开始"-"控制面板"-"管理工具" ...
- Linux grep命令分析以及C语言版本的实现
1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全 ...