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.
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if(head==null)
return null;
RandomListNode p=head;
//拷贝当前节点,并将其插入到被拷贝节点的后方
while(p!=null){
RandomListNode temp=new RandomListNode(p.label);
temp.next=p.next;
p.next=temp;
p=temp.next;
}
p=head;
//将拷贝节点的随机指针指向应有位置
while(p!=null){
if(p.random!=null){
p.next.random=p.random.next;
}
p=p.next.next;
}
RandomListNode newhead=head.next;
p=head;
//将拷贝链表与原链表分离
while(p!=null){
RandomListNode temp=p.next;
p.next=temp.next;
if(temp.next!=null)
temp.next=temp.next.next;
p=p.next;
}
return newhead;
}
}
最后分离也可这样写:
while(p != null && p.next != null){
RandomListNode temp = p.next;
p.next = temp.next;
p = temp;
}
解法2:
可以用hashmap的方式
public RandomListNode copyRandomList(RandomListNode head) {
if (head == null)
return null;
HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
RandomListNode newHead = new RandomListNode(head.label);
RandomListNode p = head;
RandomListNode q = newHead;
map.put(head, newHead);
p = p.next;
while (p != null) {
RandomListNode temp = new RandomListNode(p.label);
map.put(p, temp);
q.next = temp;
q = temp;
p = p.next;
}
p = head;
q = newHead;
while (p != null) {
if (p.random != null)
q.random = map.get(p.random);
else
q.random = null;
p = p.next;
q = q.next;
}
return newHead;
}
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 @ Python
原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...
- Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...
- 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(带random引用的单链表深拷贝)
问题: 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] 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 &Clone Graph 复杂链表的复制&图的复制
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
随机推荐
- day03_03 第一个Python程序
Python的安装 之前安装了python2.7.11,接下来的课程因为是python3的,需要再安装一个python3版本...... 1.进入python.org进行选择安装 2.或者选择课件里的 ...
- 简单使用jstl实现敏感字替换
package com.ceshi; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; ...
- java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()解决办法
代码改变世界 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pre ...
- Kafka单机配置部署
摘要:上节 学习了Kafka的理论知识,这里安装单机版以便后续的测试. 首先安装jdk 一.单机部署zk 1.1安装: tar -zxf zookeeper-3.4.10.tar.gz -C /opt ...
- HDU——1058Humble Numbers(找规律)
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- RobotFramwork自定义库
这么长时间才知道有RobotFramwork这东西... 感叹之前都干啥去了,感叹公司为啥不用这货? 网上的安装文档都有,就不用自己在记录啦. 感觉在实际实用时,肯定要有自己定义的库啊,不能只用bui ...
- jsp生成war
安装java 环境,进入jsp所在目录,使用如下命令可将当前目录中所有文件打成到xss.war包中,正常的war包中还包含另外两个文件meta-inf,web-inf,在生成的时候,需要把这两个文件加 ...
- djang中的request.user对象中的方法
print(dir(request.user)) ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', ...
- http client transfer
背景 在平时工作中我偶尔会写一些脚本监控HTTP接口的健康状况,基本上都是发送HTTP GET或HTTP POST请求,然后检测响应内容.但一直用的是WebClient和HttpWebRequest, ...
- PXC部署,配置,操作原理
参考:https://www.cnblogs.com/kevingrace/p/5685371.html?utm_source=itdadao&utm_medium=referra ...