LeetCode-M-Copy List with Random Pointe
题意
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.
Subscribe to see which companies asked this question.
解法
回头补上
实现
/*** Definition for singly-linked list with a random pointer.* struct RandomListNode {* int label;* RandomListNode *next, *random;* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}* };*/
class Solution {
public:RandomListNode *splitCopyList(RandomListNode* head){RandomListNode* copyhead = head->next;RandomListNode* cur = head;while(cur != NULL){RandomListNode* ccur = cur->next;cur->next = ccur->next;if(cur->next != NULL) ccur->next = cur->next->next;else ccur->next = NULL;cur = cur->next;}return copyhead;}RandomListNode *copyRandomRela(RandomListNode* head){RandomListNode* cur = head;while(cur != NULL){RandomListNode* random = cur->random;RandomListNode* ccur = cur->next;if(random != NULL){ccur->random = random->next;}cur = ccur->next;}return head;}RandomListNode *copyNextRela(RandomListNode* head){RandomListNode* cur = head;while(cur != NULL){RandomListNode* ccur = new RandomListNode(cur->label);ccur->next = cur->next;cur->next = ccur;cur = ccur->next;}return head;}RandomListNode *copyRandomList(RandomListNode *head) {if(head == NULL) return head;RandomListNode* nextHead = copyNextRela(head);RandomListNode* randomHead = copyRandomRela(head);RandomListNode* copyHead = splitCopyList(randomHead);return copyHead;}
};
LeetCode-M-Copy List with Random Pointe相关推荐
- 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 ...
- [LeetCode] Copy List with Random Pointe
题目的关键是要让新链表和原有链表发送关联,可以通过这种关联来设置新链表的random pointer 思路:将新链表的元素插入到原有链表元素的后面,如下图所示,就可以根据原有链表的radom-> ...
- 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 ...
- Leetcode 138 Copy List With Random Pointer
题目描述 A linked list is given such that each node contains an additional random pointer which could po ...
- 138. Copy List with Random Pointer
/** 138. Copy List with Random Pointer * 2016-5-22 by Mingyang* 要遍历两次,第一次用来找到所有的next节点,并且把新旧节点全部存在ha ...
- 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 ...
最新文章
- 2021-02-23 如何用简单易懂的例子解释条件随机场(CRF)模型?它和HMM有什么区别?从HMM、MEMM、CRF某牛自己总结的
- iptables配置管理
- 【Android 安装包优化】WebP 图片格式性能测试 ( 测试 WebP 图片解码速度 | 测试 WebP 图片编码速度 )
- python自动访问网页_Python自动化 selenium 网页自动化-在访问一个网页时弹出的浏览器窗口,我该如何处理?...
- linux启动tongweb命令,linux7开机自启动东方通tongweb
- es分页查询重复数据_ES优化 - 巨量数据如何提高查询性能
- Android中用文件初始化sqlite 数据库(二)
- Eclipse Qt开发环境的建立【转】
- LiteIDE中运行GO
- windows创建bat文件进行截图
- 大数据分析平台搭建指南
- 挑战全网目前最全python例子(附源码),独此一家,经典值得收藏!!!(五):Python绘图,Python实战
- K-means聚类攻击类型
- Wifi热点java_java实现笔记本电脑设置成WiFi热点
- 使用微擎后台授权须知
- 2019年最新的一波手机APP分享,每一个都是大家的菜哦!
- java面试题有哪些?
- python网络编程学什么_python网络编程学习《一》
- bzoj 3398: [Usaco2009 Feb]Bullcow 牡牛和牝牛
- Elasticsearch在Linux中的单节点部署和集群部署