Each of the nodes in the linked list has another pointer pointing to a random node in the list or null. Make a deep copy of the original list.

/**
* class RandomListNode {
* public int value;
* public RandomListNode next;
* public RandomListNode random;
* public RandomListNode(int value) {
* this.value = value;
* }
* }
*/
public class Solution {
public RandomListNode copy(RandomListNode head) {
// Write your solution here.
if (head == null) {
return head;
}
Map<RandomListNode, RandomListNode> map = new HashMap<>();
map.put(head, new RandomListNode(head.value));
RandomListNode dummy = new RandomListNode(0);
RandomListNode cur = dummy; while (head != null) {
if (!map.containsKey(head)) {
map.put(head, new RandomListNode(head.value));
}
// connect for the next of cur
cur.next = map.get(head);
if (head.random != null) {
if (!map.containsKey(head.random)) {
map.put(head.random, new RandomListNode(head.random.value));
}
cur.next.random = map.get(head.random);
}
head = head.next;
cur = cur.next;
}
return dummy.next;
}
}

最新文章

  1. Surface Shader简单向导
  2. JavaScript Patterns 2.10 Naming Conventions
  3. golang代码执行顺序
  4. docker居然需要3.10以上的内核
  5. Using Boost Libraries in Windows Store and Phone Applications
  6. Python入门,新手之路
  7. HDU 3932 Groundhog Build Home 【基础模拟退火】
  8. Java中abstract关键字详解
  9. Dijkstra算法 Java实现
  10. MongoDB与python交互
  11. 在React中使用Typescript的实践问题总结
  12. 软件测试2019:第四次作业—— 性能测试(含JMeter实验)
  13. 【Gym 100947I】What a Mess
  14. vue修饰符学习
  15. ado.net 中事务的使用
  16. Linux基础四(服务管理)
  17. HDU 6008 - Worried School
  18. 微信小程序注册身份证验证
  19. cocos2dx lua 图片去色shader
  20. Field &#39;email&#39; doesn&#39;t have a default value

热门文章

  1. Go语言 使用append() 为切片动态添加元素
  2. unicode字符等价探究
  3. 复选框全选、反选及根据值JS控制复选框默认选中事件
  4. JAVA作用域和排序算法介绍
  5. Python MySQL Select
  6. Node.js 发送Email
  7. 5分钟搞懂:JWT(Json Web Token)
  8. Vue 改变数组触发视图更新
  9. 关于torch.nn.Conv2d的笔记
  10. C#构造函数调用其他构造函数