深拷贝一个链表,不同的是这个链表有个额外的随机指针。参考:http://blog.csdn.net/ljiabin/article/details/39054999

做法非常的巧妙,分成三步,一是新建结点,并放在旧结点之后;二是修改新结点的random指针;三是将新旧链表断开。

RandomListNode *randomList(RandomListNode *head)
{
//复制每个结点,并将新结点放在旧结点之后
for (RandomListNode *cur = head; cur != nullptr;)
{
RandomListNode *node = new RandomListNode(cur->label);
node->next = cur->next;
cur->next = node;
cur = node->next;
} //修改新结点的Random指针
for (RandomListNode *cur = head; cur != nullptr;)
{
if (cur->random != nullptr)cur->next->random = cur->random->next; cur = cur->next->next;
} //将新旧链表断开
RandomListNode dummy(-);
for (RandomListNode *cur = head,*new_cur=&dummy; cur != nullptr;)
{
new_cur->next = cur->next;
new_cur = new_cur->next; cur->next = cur->next->next;
cur = cur->next; } return dummy.next;
}

leetcode 之Copy List with Random Pointer(23)的更多相关文章

  1. [Leetcode Week17]Copy List with Random Pointer

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

  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 拷贝带随机指针的链表

    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 (hard)

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

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

  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

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

  9. leetcode 【 Copy List with Random Pointer 】 python 实现

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

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

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

随机推荐

  1. BZOJ3144:[HNOI2013]切糕——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3144 看着很像网络流,但是费用流貌似无法解决这个问题,其实甚至连忽略d的情况都做不到. 最小割? ...

  2. C++中typedef和#define简介

    本文基于<C++ Primer(第5版)>和网上博客,整理而成. 一.类型别名 类型别名是一个名字,它是某种类型的同义词,有两种方法可用于定义类型别名:typedef.using. 1.关 ...

  3. bzoj1004: [HNOI2008]Cards(burnside引理+DP)

    题目大意:3种颜色,每种染si个,有m个置换,求所有本质不同的染色方案数. 置换群的burnside引理,还有个Pólya过几天再看看... burnside引理:有m个置换k种颜色,所有本质不同的染 ...

  4. Star sky 二维前缀和

    C. Star sky time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  5. [mysql]数据库引擎查看

    1.查看数据库引擎 全局下,show engines; 2.察看数据库引擎 show variables like '%engine%'; 或者show create table xxx\G 会显示默 ...

  6. unix awk手册读书笔记

    http://note.youdao.com/noteshare?id=9ac76eb63a53ac000f7814454642d2b0

  7. JQuery学习五

    获取样式attr("myclass")移除样式removeClass("myclass")增加样式addClass("myclass")to ...

  8. 珠排序Bead Sort

    珠排序非常另类[地精也很另类],看完你就知道了,先介绍思路,再分解过程 这是它的英文论文 http://www.cs.auckland.ac.nz/~jaru003/research/publicat ...

  9. Maven-Dependency Mechanism

    依赖管理是maven的一个广为人知的特性, 这也是maven擅长的一个领域. 为单一的工程管理依赖不是很难, 但当你着手处理多模块工程和包含数十或数百个模块的应用时, maven可以帮助你很好地保持高 ...

  10. elasticsearch中Node的创建

    要连接到集群,首先要告诉集群:你是谁,你有什么特征.在es中体现为实例化节点,elasticsearch通过org.elasticsearch.node.NodeBuilder的build()或者no ...