题目

给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。

要求返回这个链表的深度拷贝。


考点


思路


代码

/**
* 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) {
RandomListNode* ptr=head;
RandomListNode* new_head=NULL;
std::vector<RandomListNode*> node_vec;
std::map<RandomListNode*,int>node_map;
int i=0; while(ptr){ node_map[ptr]=i;
node_vec.push_back(new RandomListNode(ptr->label)); i++;
ptr=ptr->next;
} ptr=head;
node_vec.push_back(0);
i=0;
while(ptr){
node_vec[i]->next=node_vec[i+1];
if(ptr->random){
int id=node_map[ptr->random];
node_vec[i]->random=node_vec[id];
}
i++;
ptr=ptr->next;
}
return node_vec[0];
}
};

问题

第35题:LeetCode138. Copy List with Random Pointer的更多相关文章

  1. Leetcode138. Copy List with Random Pointer复制带随机指针的链表

    给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深度拷贝. 方法一: class Solution { public: RandomLis ...

  2. 16. Copy List with Random Pointer

    类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...

  3. 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

    133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...

  4. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  5. Copy List with Random Pointer leetcode java

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  6. LintCode - Copy List with Random Pointer

    LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...

  7. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

  8. [Java]LeetCode138. 复制带随机指针的链表 | Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  9. LeetCode138:Copy List with Random Pointer

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

随机推荐

  1. 【input】——数据传入后台

    1.复选框 checkbox <label class="checkbox"> <input type="checkbox" name=&qu ...

  2. 祝高二学弟学妹AK NOIp2018!!!!!!

         

  3. H-ui出现提交后没办法关闭

    可以用sublime代替服务器来解决,或者是webstorm可以自行搭建服务器来解决当前的问题. sublime可以更改端口号 自己加上一个服务器 默认打开浏览器的 “快捷键”

  4. Java 之 Serializable 序列化和反序列化的概念,作用的通俗易懂的解释

    遇到这个 Java Serializable 序列化这个接口,我们可能会有如下的问题a,什么叫序列化和反序列化b,作用.为啥要实现这个 Serializable 接口,也就是为啥要序列化c,seria ...

  5. 零度4W1H提问规则

    WHAT:您现在的需求和目的是什么,请按条理描述清楚. WHERE:在什么平台.环境和工具下发生此问题. WHEN:何时发生的该问题,该问题是否能够重现. WHY:为什么不能通过搜索引擎来解决您的问题 ...

  6. open ssh 常用的东西

    清除已经存在的但是不同设备的连接信息 ssh-keygen -f "/users/he/.ssh/known_hosts" -R 192.168.1.118 无密码登录openss ...

  7. java中空字符串、null的区别

    String 的null,或者赋值为"",有什么区别? 废话少说,上代码: public class EmptyAndNull { /** * @param args */ pub ...

  8. 【CSS】等高布局

    1. 负margin:   margin-bottom:-3000px; padding-bottom:3000px; 再配合父标签的overflow:hidden属性即可实现高度自动相等的效果.   ...

  9. My First Blog in Cnblogs

    终于打算从csdn搬到博客园了 虽然在csdn只写过三篇文章,不过打算写第四篇的时候发现原先的三篇都消失了.联系客服最终还是找回了,不过对于csdn神奇的管理方式还是不放心,也没在csdn上再写过文章 ...

  10. 腾讯CodeStar第二季前端突击队腐蚀的画解法步骤笔记

    所有题目地址:http://codestar.alloyteam.com/q2 本题内容:http://www.cnblogs.com/yedeying/p/3617593.html 腐蚀的画涉及到的 ...