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的更多相关文章

  1. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  2. [leetcode]Copy List with Random Pointer @ Python

    原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: A linked list is given such ...

  3. Leetcode Copy List with Random Pointer(面试题推荐)

    给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...

  4. LeetCode——Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  5. LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)

    问题: A linked list is given such that each node contains an additional random pointer which could poi ...

  6. Leetcode Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  7. [Leetcode] Copy list with random pointer 对带有任意指针的链表深度拷贝

    A linked list is given such that each node contains an additional random pointer which could point t ...

  8. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  9. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

随机推荐

  1. python学习-- Django传递数据给JS

    var List = {{ List|safe }};//safe 必须存在

  2. SDOJ 3696 Tree

    描述 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. 输入 第一行V,E,need分别表示点数,边数和需要的白色边数. 接下来E行 每行 ...

  3. Latex数学公式表

    1. Latex的两种公式模式 行间(inline)模式:即在正文中插入数学内容.行间公式用$ … $ 独立(display)模式:独立成行,可以有或没有编号.无编号用\ [ … \ ] 2.基本元素 ...

  4. 九度oj 题目1470:调整方阵

    题目描述: 输入一个N(N<=10)阶方阵,按照如下方式调整方阵:1.将第一列中最大数所在的行与第一行对调.2.将第二列中从第二行到第N行最大数所在的行与第二行对调. 依此类推...N-1.将第 ...

  5. Android中当前墙纸Wallpaper存放的位置

    最近想做个应用保存当前墙纸,找了一下,发现当前墙纸的位置在. /System/users/0/wallpaper 没有后缀.导出来修改一下名字就可以看到图标了.比如改为png. 但是,这个目录要求系统 ...

  6. hibernate基础工具findBySQL学习

    public List<Map<String,Object>> findBySQL(String sql,Map<String,Object> param,int ...

  7. 设计模式(Java随笔)—装饰模式

    装饰模式(Decorator Pattern):为已有功能动态地添加更多功能的一种方式Attach additional responsiblities to an object dynamicall ...

  8. Codeforces963B - Destruction of a Tree

    Portal Description 给出一个\(n(n\leq2\times10^5)\)个点的树,每次可以删除一个度数为偶数的点及其相连的边,求一种能够删掉整棵树的方案. Solution 简单起 ...

  9. 刷题总结——Tree2cycle(hdu4714 树形dp)

    题目: A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 unit of ...

  10. log4j.xml——java日志处理组件配置简介

    (从一篇好文开始)log4j(一)——为什么要用log4j? 三:看完栗子后的感想 (1)很明显我们在编写代码的时候有各种需要打印日志的需求,比如:我们调试代码的时候:我们的应用出现了问题,我们分析. ...