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- ...
随机推荐
- python学习--Django mvc框架简介
让我们一览 Django 全貌 urls.py 网址入口,关联到对应的views.py中的一个函数(或者generic类),访问网址就对应一个函数. views.py 处理用户发出的请求,从urls. ...
- 2.启动ABP ASP.NET ZERO
1.使用VS2017打开项目,等待自动还原程序包结束 2.生成项目,确保项目全部生成成功 3.生成数据库 (1).将项目“MyCompanyName.AbpZeroTemplate.EntityFra ...
- C#发送邮件异常,返回信息乱码
发邮件时出现了异常: 在 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) 在 ...
- JDBC 学习笔记(八)—— ResultSet
JDBC 使用 ResultSet 来封装 SQL 的查询结果,可以将 ResultSet 类比为数据库表的查询结果. 它拥有如下两个性质: 可滚动. 可更新. 这两个性质,是在创建 Statemen ...
- Apache2 FastCGI C Demo
安装依赖 sudo apt-get install libapache2-mod-fastcgi a2enmod fastcgi sudo apt-get install libfcgi-dev li ...
- <定时主库导出/备库导入>
1.设置定时任务时间及所需要的dmp文件路径 [mm1@localhost ~]$ crontab -e 0 0 * * * sh /home/mm1/exp_table.sh 2>& ...
- 线程与threading模块
线程 进程内一个相对独立的.可调度的执行单元,是系统独立调度和分派CPU的基本单位.在单个进程中同时运行多个线程完成不同的工作,称为多线程. 同一进程内的多个线程是共享该进程的资源. 创建新的线程开销 ...
- foj 2150 bfs
题意: 给定一个平面图 . 为空地(不着火) # 为草 开始可以选1-2个草堆点燃,每隔一秒会把上下左右的草引燃(开始时间为0秒) 问把所有草烧光的最少时间 #include<iostream& ...
- Linux System Programming 学习笔记(十) 信号
1. 信号是软中断,提供处理异步事件的机制 异步事件可以是来源于系统外部(例如用户输入Ctrl-C)也可以来源于系统内(例如除0) 内核使用以下三种方法之一来处理信号: (1) 忽略该信号.SIG ...
- net1:DateTime,Application与Session,
原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...