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.
原题链接:https://oj.leetcode.com/problems/copy-list-with-random-pointer/
题目:给定一个链表,当中的每一个节点包括有一个额外的随机指针指向链表中的随意其它节点或空。
返回链表的一份深度复制。
思路:复制源链表中的每个节点到新链表中(仅仅考虑next)。复制随机指针关系到新链表中(仅仅考虑random.next),此时新链表的长度是源链表的2倍了。此时修正随机指针为正确的指向关系。
	public RandomListNode copyRandomList(RandomListNode head) {
		if(head == null)
			return null;
		RandomListNode p = head;
		while(p != null){
			RandomListNode copy = new RandomListNode(p.label);
			copy.next = p.next;
			p.next = copy;
			p = copy.next;
		}
		p = head;
		while(p != null){
			if(p.random != null)
				p.next.random = p.random.next;
			p = p.next.next;
		}
		p = head;
		RandomListNode newHead = head.next;
		while(p != null){
			RandomListNode tmp = p.next;
			p.next = tmp.next;
			if(tmp.next != null)
				tmp.next = tmp.next.next;
			p = p.next;
		}
		return newHead;
	}
	// Definition for singly-linked list with a random pointer.
	class RandomListNode {
		int label;
		RandomListNode next, random;
		RandomListNode(int x) {
			this.label = x;
		}
	}
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(带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
		
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- ...
 
随机推荐
- [Android中级]使用Commons-net-ftp来实现FTP上传、下载的功能
			
本文属于学习分享,如有雷同纯属巧合 利用业余时间.学习一些实用的东西,假设手又有点贱的话.最好还是自己也跟着敲起来. 在android上能够通过自带的ftp组件来完毕各种功能.这次是由于项目中看到用了 ...
 - Hive权限之改进
			
不足 即使开启hive权限认证的情况下,不论什么用户仍然是超级用户.能够通过grant给不论什么人赋予不论什么权限,这样权限认证基本没有意义.因此必须在开启权限认证的同一时候.对运行grant/rev ...
 - Codeforces Round #338 (Div. 2)  B. Longtail Hedgehog 记忆化搜索/树DP
			
B. Longtail Hedgehog This Christmas Santa gave Masha a magic picture and a pencil. The picture con ...
 - 0x08 总结与练习
			
1:前面已经搞好了. 2:poj2965 这种开关问题一个点要么点一次要么不点,枚举所有点的方案实行即可 #include<cstdio> #include<iostream> ...
 - Navicat Premium 12 模型导出sql
			
找了半天,终于找到导出sql了!
 - [jzoj 6092] [GDOI2019模拟2019.3.30] 附耳而至 解题报告 (平面图转对偶图+最小割)
			
题目链接: https://jzoj.net/senior/#main/show/6092 题目: 知识点--平面图转对偶图 在求最小割的时候,我们可以把平面图转为对偶图,用最短路来求最小割,这样会比 ...
 - WPF向系统发送消息 并传递结构体
			
场景 :需要开发一个通讯组件 流程为:界面-开启接收服务-通过发送组件发送信息到 其他客户端和服务端 接受服务接收其他客户端发送的消息 需要传递给对应组件或者界面 因此会出现类库重复引用问题.因为采用 ...
 - R 连接DB2数据库
			
1.odbc文件下载 教程: http://dasapp.oregon.gov/datamart/files/IBM_DB2_9.7_Run_Time_client_Notes.pdf 驱动地址: h ...
 - web开发必看:你的网站支持https吗?
			
如果有一项技术可以让网站的访问速度更快.更安全.并且seo权重提升(百度除外),而且程序员不需要改代码就可以全站使用,最重要的是,不需要额外花钱,那有这么好的事情吗? HTTP通信协议是全球万维网ww ...
 - echart的tooltip自定义换行
			
自定义换行,内容很长的时候 tooltip : { trigger: 'axis', axisPointer : { // 坐标轴指示器,坐标轴触发有效 type : 'shadow' // 默认为直 ...