深拷贝一个链表,不同的是这个链表有个额外的随机指针。参考: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. BZOJ3329:Xorequ——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=3329 原式化为x^2x=3x,而且实际上异或就是不进位的加法. 那么我们又有x+2x=3x,所以在做 ...

  2. bzoj3680: 吊打XXX(模拟退火)

    题目要求 最小(dis表示绳结到点i的距离),就是个广义费马点的题,模拟退火裸题QAQ 模拟退火就是优化后的爬山算法,一开始先随机一个平均点,接下来如果随机到的点比当前点劣,温度比较高的话也有几率跳过 ...

  3. 【简单算法】17.字符串转整数(atoi)

    题目: 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为 ...

  4. mysql语句进阶

    1.null mysql> create table worker(id int not null,name varchar(8) not null,pass varchar(20) not n ...

  5. memchr函数

    函数原型:extern void *memchr(void *str, char ch, unsigned count) 参数说明:从str所指内存区域的前count个字节查找字符ch.        ...

  6. js 获取当前链接和获取域名

    <script language="javascript"> //获取域名 host = window.location.host; host2=document.do ...

  7. c# 合并两个有序数组

    , , , , , }; , , , }; ArrayList lists = new ArrayList(); ArrayList temp = new ArrayList(); lists.Add ...

  8. Robotframework Web自动化实战课程

    想学习的小伙伴,现在可以报名了!!!7月1日正式开课本期课程主要是web自动化为主,根据平时工作经验整理的一套流程以及使用过程中常见的问题总结.学完后能很快上手,即学即用,课后遇到问题在线解答,远程协 ...

  9. Ubuntu12.04 GIT安装和使用

    一.安装GIT和配置GIT 1.安装GIT apt-get install git 2.配置GIT ##配置用户信息 git config --global user.name "John ...

  10. 元类编程-- metaclass

    #类也是对象,type创建类的类 def create_class(name): if name == "user": class User: def __str__(self): ...