Copy List with Random Pointer
2015-07-18 12:22
246 查看
称号
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.
方法
public RandomListNode copyRandomList(RandomListNode head) { RandomListNode newHead = null; if (head != null) { RandomListNode cur = head; while (cur != null) { RandomListNode temp = new RandomListNode(cur.label); temp.next = cur.next; cur.next = temp; cur = cur.next.next; } cur = head; RandomListNode newCur = head.next; while(cur != null) { if (cur.random != null) { newCur.random = cur.random.next; } cur = cur.next.next; if (cur != null) { newCur = newCur.next.next; } } cur = head; newHead = cur.next; newCur = newHead; while (cur != null) { cur.next = newCur.next; cur = newCur.next; if (cur != null) { newCur.next = cur.next; newCur = cur.next; } else { newCur.next = null; } } } return newHead; }
相关文章推荐
- Nginx源代码分析之upstream(八)
- 架构设计:负载均衡层设计方案(4)——LVS原理
- 如何在默认安装openjdk的Linux系统中安装独立JDK
- 论坛:Error:No result defined for action cn.itcast.oa.view.action.TopicAction and result
- linux命令
- 图解openssl实现私有CA
- Eclipse LUNA配置TomCat(非j2ee版本)
- 图解openssl实现私有CA
- 织梦网站底部的Power by&nbsp…
- ecshop伪静态问题
- PR值与网站权重的关系
- ecshop模板循环foreach的使用方法…
- 网站后门的检测与清除
- ecshop验证码显示不了怎么办?
- Chrome浏览器模拟手机访问网站的设…
- Chrome浏览器模拟手机访问网站的设…
- 【ecshop】ECshop2.72修改版权及标…
- 怎么才能让网站的图片不能被游客下…
- ECshop出现错误提示 Warning:…
- ECSHOP注册时增加推荐人