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.

  1. /**
  2. * Definition for singly-linked list with a random pointer.
  3. * class RandomListNode {
  4. * int label;
  5. * RandomListNode next, random;
  6. * RandomListNode(int x) { this.label = x; }
  7. * };
  8. */
  9. public class Solution {
  10. public RandomListNode copyRandomList(RandomListNode head) {
  11. if(head==null)
  12. return null;
  13. RandomListNode p=head;
    //拷贝当前节点,并将其插入到被拷贝节点的后方
  14. while(p!=null){
  15. RandomListNode temp=new RandomListNode(p.label);
  16. temp.next=p.next;
  17. p.next=temp;
  18. p=temp.next;
  19. }
  20. p=head;
    //将拷贝节点的随机指针指向应有位置
  21. while(p!=null){
  22. if(p.random!=null){
  23. p.next.random=p.random.next;
  24. }
  25. p=p.next.next;
  26. }
  27. RandomListNode newhead=head.next;
  28. p=head;
    //将拷贝链表与原链表分离
  29. while(p!=null){
  30. RandomListNode temp=p.next;
  31. p.next=temp.next;
  32. if(temp.next!=null)
  33. temp.next=temp.next.next;
  34. p=p.next;
  35. }
  36. return newhead;
  37. }
  38. }

最后分离也可这样写:

  1. while(p != null && p.next != null){
  2. RandomListNode temp = p.next;
  3. p.next = temp.next;
  4. p = temp;
  5. }

解法2:

可以用hashmap的方式

  1. public RandomListNode copyRandomList(RandomListNode head) {
  2. if (head == null)
  3. return null;
  4. HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
  5. RandomListNode newHead = new RandomListNode(head.label);
  6.  
  7. RandomListNode p = head;
  8. RandomListNode q = newHead;
  9. map.put(head, newHead);
  10.  
  11. p = p.next;
  12. while (p != null) {
  13. RandomListNode temp = new RandomListNode(p.label);
  14. map.put(p, temp);
  15. q.next = temp;
  16. q = temp;
  17. p = p.next;
  18. }
  19.  
  20. p = head;
  21. q = newHead;
  22. while (p != null) {
  23. if (p.random != null)
  24. q.random = map.get(p.random);
  25. else
  26. q.random = null;
  27.  
  28. p = p.next;
  29. q = q.next;
  30. }
  31.  
  32. return newHead;
  33. }

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 mvc框架简介

    让我们一览 Django 全貌 urls.py 网址入口,关联到对应的views.py中的一个函数(或者generic类),访问网址就对应一个函数. views.py 处理用户发出的请求,从urls. ...

  2. 2.启动ABP ASP.NET ZERO

    1.使用VS2017打开项目,等待自动还原程序包结束 2.生成项目,确保项目全部生成成功 3.生成数据库 (1).将项目“MyCompanyName.AbpZeroTemplate.EntityFra ...

  3. C#发送邮件异常,返回信息乱码

    发邮件时出现了异常: 在 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) 在 ...

  4. JDBC 学习笔记(八)—— ResultSet

    JDBC 使用 ResultSet 来封装 SQL 的查询结果,可以将 ResultSet 类比为数据库表的查询结果. 它拥有如下两个性质: 可滚动. 可更新. 这两个性质,是在创建 Statemen ...

  5. Apache2 FastCGI C Demo

    安装依赖 sudo apt-get install libapache2-mod-fastcgi a2enmod fastcgi sudo apt-get install libfcgi-dev li ...

  6. <定时主库导出/备库导入>

    1.设置定时任务时间及所需要的dmp文件路径 [mm1@localhost ~]$ crontab -e 0 0 * * *  sh /home/mm1/exp_table.sh  2>& ...

  7. 线程与threading模块

    线程 进程内一个相对独立的.可调度的执行单元,是系统独立调度和分派CPU的基本单位.在单个进程中同时运行多个线程完成不同的工作,称为多线程. 同一进程内的多个线程是共享该进程的资源. 创建新的线程开销 ...

  8. foj 2150 bfs

    题意: 给定一个平面图 . 为空地(不着火) # 为草 开始可以选1-2个草堆点燃,每隔一秒会把上下左右的草引燃(开始时间为0秒) 问把所有草烧光的最少时间 #include<iostream& ...

  9. Linux System Programming 学习笔记(十) 信号

    1. 信号是软中断,提供处理异步事件的机制 异步事件可以是来源于系统外部(例如用户输入Ctrl-C)也可以来源于系统内(例如除0)   内核使用以下三种方法之一来处理信号: (1) 忽略该信号.SIG ...

  10. net1:DateTime,Application与Session,

    原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...