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.

随机链表的节点数据结构

struct RandomListNode {
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
};

注意本题是对带有随机链表指针的深度拷贝,

如果数据结构中那个没有随机指针,只需要将链表遍历拷贝一遍即可

但题目给的数据结构含有指针,拷贝后就必须保有随机指针的关系

本题通过在每个节点之后插入一个当前节点的拷贝节点,然后让插入的节点保有当前节点随机指针的关系,最后将插入的节点从链表中剥离出来即可

主要分为3步

(1)插入拷贝的节点

(2)复制random指针

(3)将插入的节点分离出来

/**
* 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 *copyRandomList(RandomListNode *head) {
//copy list
RandomListNode *p = head;
while(p){
RandomListNode *tmp = new RandomListNode(p->label);
tmp->next = p->next;
p->next = tmp;
p = tmp->next;
}
//copy random pointer
RandomListNode *q = head;
while(q){
RandomListNode *tmp = q->next;
if(q->random){
tmp->random = q->random->next;
}
q=tmp->next;
}
//seperate list
RandomListNode *dupHead = head == NULL ? NULL : head->next, *cur = head;
while(cur){
RandomListNode *tmp = cur->next;
cur->next = tmp->next;
if(tmp->next){
tmp->next = tmp->next->next;
}
cur = cur->next;
}
return dupHead;
}
};

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. windows bat 设置代理上网脚本bat

    取消IE代理服务器 ****************************************************************************************** ...

  2. My97DatePicker

    http://www.my97.net/index.asp <input id="txtDate" class="Wdate" type="te ...

  3. SQL case

    case when Value='1' then 'True'  else 'False' end as 'Result'

  4. Pjax调用

    $.pjax({container:'#content_center',url:href,data:data}); $(document).on('pjax:send', function() {// ...

  5. jquery ajax beforeSend 提交等待问题

    需要使用异步加载async : true 否则不会出现等待效果 $.ajax({ url : $('#form').attr("action"), data: $('#form') ...

  6. SQL2005解密已经被加密的存储过程

    SQL2005解密已经被加密的存储过程 第一步:打开DAC连接功能 第二步:在MASTER数据库创建一个解密存储过程 USE master GO CREATE PROCEDURE [dbo].[sp_ ...

  7. Symbiont

    http://www.weiyangx.com/209230.html Symbiont,Credit Suisse与R3携手革新贷款数据验证环节Symbiont, Credit Suisse and ...

  8. 【JWT】JWT+HA256加密 Token验证

    目录 Token验证 传统的Token验证 JWT+HA256验证 回到顶部 Token验证 最近了解下基于 Token 的身份验证,跟大伙分享下.很多大型网站也都在用,比如 Facebook,Twi ...

  9. Coursera-Getting and Cleaning Data-Week3-dplyr+tidyr+lubridate的组合拳

    Coursera-Getting and Cleaning Data-Week3 Wednesday, February 04, 2015 好久不写笔记了,年底略忙.. Getting and Cle ...

  10. 微信公众平台可通过UnionID机制在多公众号间帐号互通

    微信公众平台越来越注重开发者的反馈信息了,这不,微信开放平台提供了UnionID机制,通过获取用户基本信息接口,开发者可通过OpenID来获取用户基本信息,而如果开发者拥有多个公众号,可使用以下办法通 ...