原题地址

非常巧妙的方法,不需要用map,只需要O(1)的额外存储空间,分为3步:

1. 先复制链表,但是这个复制比较特殊,每个新复制的节点添加在原节点的后面,相当于"加塞"
2. 根据原节点的 ramdon 指针构造新节点的 random 指针
3. 恢复原链表结构,同时得到新复制链表

时间复杂度:O(n)

注意random有可能是NULL

代码:

 RandomListNode *copyRandomList(RandomListNode *head) {
RandomListNode *h = NULL; h = head;
while (h) {
RandomListNode *node = new RandomListNode(h->label);
node->next = h->next;
h->next = node;
h = h->next->next;
} h = head;
while (h) {
h->next->random = h->random? h->random->next : NULL;
h = h->next->next;
} h = head? head->next : NULL;
while (head) {
RandomListNode *tmp = head->next;
head->next = head->next->next;
tmp->next = head->next ? head->next->next : NULL;
head = head->next;
} return h;
}

Leetcode#138 Copy List with Random Pointer的更多相关文章

  1. [LeetCode] 138. Copy List with Random Pointer 拷贝带有随机指针的链表

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

  2. Java for LeetCode 138 Copy List with Random Pointer

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

  3. [LeetCode] 138. Copy List with Random Pointer 拷贝带随机指针的链表

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

  4. leetcode 138. Copy List with Random Pointer ----- java

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

  5. leetcode 138. Copy List with Random Pointer复杂链表的复制

    python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...

  6. [leetcode]138. Copy List with Random Pointer复制带有随机指针的链表

    public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, ...

  7. 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 ...

  8. [Leetcode Week17]Copy List with Random Pointer

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

  9. 【LeetCode】138. Copy List with Random Pointer

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

随机推荐

  1. JS获取网页属性包括宽、高等

    JS获取网页属性包括宽.高等. function getInfo()  { // www.jbxue.com var s = "";  s += " 网页可见区域宽:&q ...

  2. Delphi CxGrid 汇总(3)

    列   解决:       <aColumn>.GroupIndex   :=   -1;         <aColumn>.Visible   :=   True; *** ...

  3. FileUpload控件「批次上传 / 多档案同时上传」的范例--以「流水号」产生「变量名称」

    原文出處  http://www.dotblogs.com.tw/mis2000lab/archive/2013/08/19/multiple_fileupload_asp_net_20130819. ...

  4. python 使用联动优势支付接口的sign与verify

    直接上代码 if options.umpay_private_key is not None and len(options.umpay_private_key) > 0: try: with ...

  5. Should .close() be put in finally block or not?

    The following are 3 different ways to close a output writer. The first one puts close() method in tr ...

  6. 鲁棒性是 Robustness

    鲁棒性是 Robustness 的音译,是指当系统受到不正常干扰时,是否还能保证主体功能正常运作.可参考 维基百科:http://zh.wikipedia.org/zh/ 鲁棒性 _( 计算机科学 ) ...

  7. iOS进阶学习-数据库

    一.数据库管理系统 1.SQL语言概述:SQL是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言. 2.常见 ...

  8. sharepoint 2010 误删除AD组用户不能访问

    不小心误操作把ad中的组删除了,在sharepoint中是通过组给的权限,在ad中新建了一个同样名的组给了权限组下面的用户还是不能访问. 解决方法: 在sharepoint中把这组从网站集中删除,重新 ...

  9. Thread系列——Thread.Sleep(0)

    转载自:http://www.cnblogs.com/ATually/archive/2010/10/21/1857261.html 线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执 ...

  10. Oracle无法drop用户,提示有连接不能删除时

    百度了一下,这个可以行得通 select username,sid,serial# from v$session alter system kill SESSION '133,169' ; drop ...