[LeetCode] Copy List with Random Pointe
题目的关键是要让新链表和原有链表发送关联,可以通过这种关联来设置新链表的random pointer
思路:将新链表的元素插入到原有链表元素的后面,如下图所示,就可以根据原有链表的radom->next 就是新链表的random指针
所以分3步骤:
1 新元素插入
2 设置新链表的random
3 拆分大链表,回复old link 和new link
1 /** 2 * Definition for singly-linked list with a random pointer. 3 * struct RandomListNode { 4 * int label; 5 * RandomListNode *next, *random; 6 * RandomListNode(int x) : label(x), next(NULL), random(NULL) {} 7 * }; 8 */ 9 10 class Solution { 11 public: 12 RandomListNode *copyRandomList(RandomListNode *head) { 13 14 RandomListNode* pOld = head; 15 RandomListNode* pNew = NULL; 16 RandomListNode* pRes = NULL; 17 18 if(head == NULL) return NULL; 19 20 // insert every new node after old new node 21 while(pOld) 22 { 23 pNew = new RandomListNode(pOld->label); 24 if(pOld == head) pRes = pNew; 25 pNew->next = pOld->next; 26 pOld->next = pNew; 27 pOld = pNew->next; 28 } 29 30 31 pOld = head; 32 // constrct new's random pointer 33 while(pOld) 34 { 35 pNew = pOld->next; 36 if(pOld->random == NULL) 37 pNew->random == NULL; 38 else 39 pNew->random = pOld->random->next; 40 pOld = pNew->next; 41 } 42 43 44 // recover old's and new's next pointer 45 //恢复old list 和new list 46 47 pOld = head; 48 49 while(pOld) 50 { 51 pNew = pOld->next; 52 if(pNew == NULL) 53 pOld->next = NULL; 54 else 55 pOld->next = pNew->next; 56 pOld = pNew; 57 } 58 59 return pRes; 60 } 61 };
转载于:https://www.cnblogs.com/diegodu/p/3777469.html
[LeetCode] Copy List with Random Pointe相关推荐
- 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 138. Copy List with Random Pointer
LeetCode 138. Copy List with Random Pointer 参考链接:http://www.cnblogs.com/grandyang/p/4261431.html Sol ...
- leetcode -day8 Copy List with Random Pointer Single Number I II
五一中间断了几天,开始继续... 1. Copy List with Random Pointer A linked list is given such that each node cont ...
- 138. Copy List with Random Pointer
/** 138. Copy List with Random Pointer * 2016-5-22 by Mingyang* 要遍历两次,第一次用来找到所有的next节点,并且把新旧节点全部存在ha ...
- leetcode 138. Copy List with Random Pointer | 138. 复制带随机指针的链表(复杂链表的复制)
题目 https://leetcode.com/problems/copy-list-with-random-pointer/ 题解 复杂链表的复制,经典问题,考察与 HashMap 的结合.注意如果 ...
- LeetCode OJ - Copy List with Random Pointer
题目: 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 ...
最新文章
- (王道408考研数据结构)第八章排序-第四节2:快速排序
- hypermesh安装包百度云_新闻突然!全新百度网盘发布
- 环境配置:Qt5.5+VS2013+OpenCv3.1.0+Win7(64bit)
- Codeforces 510 E. Fox And Dinner
- 代码投毒、删库跑路,开源生态链安全该如何保证?
- 你不知道的JavaScript(上中下三册) 电子版高清PDF -- 百度云下载
- vue中安装axios
- lol一直显示服务器满,LOL服务器爆满!排队两个小时你敢信?网友感慨原来大家都在啊...
- 容安馆札记 606-610则 笺疏
- Android 8.0 解决不能自动安装APK问题(完美适配)
- 统计独立访客需求mysql_网站流量分析项目day04
- MS5837-30BA 水深水压传感器简单介绍
- 鹅厂流出两份Android Framework技术宝典火了,完整版 PDF 限时开放下载
- Xilinx基于PCIE的部分重配置实现(一)
- 【ARM】ARM体系与计算机组成——第一篇
- 用trie树实现输入提示功能,输入php函数名,提示php函数
- 中国各城市经纬度数据
- 求方程的解(简化版)
- 北京/上海内推 | 商汤科技SenseParrots团队招聘深度学习训练框架产品经理
- SPSS面积图【012-3期】
热门文章
- 《HeadFirst设计模式》第九章-2组合模式
- javascript中的Reflect对象是什么?如何使用Reflect?
- Qt中实时调取摄像头(利用OpenCV)
- 刷新adapte要注意的地方,和adapter使用的流程
- 安装 pnpm 出现 : 无法加载文件 D:\node\node_global\pnpm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft. 解决办法
- Android 蓝牙 A2DP基础概念、A2DP音频流的建立及传输流程、A2DP播放暂停音乐命令交互过程分析 - 史上最全分析
- 「三代组装」Pacbio组装后如何用自身数据进行polish(更新版)
- 即时通讯技术文集(第16期):IM架构设计技术精选(第一部分) [共17篇]
- 计算机软件系统的分成,计算机的软件系统一般分为
- Spring初始化 - XmlWebApplicationContext