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- ...
随机推荐
- 深入新版BS4源码 探索flex和工程化sass奥秘
你可能已经听说了一个“大新闻”:Bootstrap4 合并了代号为#21389的PR,宣布放弃支持IE9,并默认使用flexbox弹性盒模型.这标志着:1)前端开发全面步入“现代浏览器”的时代进一步来 ...
- [python IO学习篇] [打开包含中文路径的文件]
https://my.oschina.net/mcyang000/blog/289460 打开路径含有中文的文件时,要注意: 1 在windows对文件名编码是采用gbk等进行编码保存,所以要将文 ...
- 101 Hack 50
101 Hack 50 闲来无事.也静不下心,打个代码压压压惊 Hard Questions by kevinsogo Vincent and Catherine are classmates who ...
- 在springmvc中使用@PathVariable时,应该注意点什么?
导读:近来在做库存调剂系统时,我从前台到后台的传值方式,主要包括:1个,用@PathVariable或者@RequestParam从路径取:大于一个,用于更新或者添加操作的,我用的是表单实体传到后台: ...
- JDBC 学习笔记(七)—— CallableStatement
在大型关系型数据库中,有一组为了完成特定功能的 SQL 语句集被称为存储过程(Stored Procedure),它是数据库中的对象. JDBC 使用 CallableStatement 对象,完成对 ...
- 设计模式(一)单例模式:1-饿汉模式(Eager)
思想: 饿汉模式是最常提及的2种单例模式之一,其核心思想,是类持有一个自身的 instance 属性,并且在申明的同时立即初始化. 同时,类将自身的构造器权限设为 private,防止外部代码创建对象 ...
- NOJ——1658平方和(自然数平方和公式和取模法则)
[1658] 平方和 时间限制: 1000 ms 内存限制: 65535 K 问题描述 给你两个数n和m,求从6开始到6*n的等差数列(差值为6)的每一项的平方的和除6模m的值 (例如n=2,m=3, ...
- BZOJ-3040 最短路
最短路+堆优化. 普通的堆还不行,自己用的是配对堆(貌似斐波那契堆也行?毕竟理论复杂度) 然后发现自己的配对堆比云神的不知快了多少...我照着他的模版打的喂.. 然后发现前T条边不理都能A... 数据 ...
- [转] Makefile 基础 (3) —— Makefile 书写规则
该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客:(最原始版本) http://blog.csdn.net/haoel/article/details/2886 我转自 ...
- 【python接口自动化】logger
#! /usr/bin/env python # coding=GBK import logging, os class Logger: def __init__(self, path, clevel ...