Copy List with Random Pointer

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.

题目意思:

深拷贝一个给定的带random指针的链表,这个random指针可能会指向其他任意一个节点或者是为null。

(又是链表啊啊啊啊啊啊啊!!!!!!!!Orz……)

解题思路:

在每个原来节点的后面插入一个新节点(label值一样),然后复制原来节点的random指针,最后包含新旧链表的长链表分成一个原来的链表和一个新的链表。

参考这里面的两个图比较容易理解~……

有一点需要注意:就是遇到链表的问题时,一定要考虑p是空指针时,不要出现p->next之类的调用,本题也是一样要考虑的。

代码如下:

 class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if(head == NULL)
return NULL;
RandomListNode *p = head;
//第一遍:扫描顺序复制next指针
while(p){
RandomListNode *newNode = new RandomListNode(p->label);
newNode->next = p->next;
p->next = newNode;
p = newNode->next;
}
p = head;
//第二遍:复制random指针
while(p){
if(p->random)
p->next->random = p->random->next;
p = p->next->next;
}
p = head;
//第三遍:恢复旧链表和新链表
RandomListNode *newHead = p->next;
RandomListNode *newP = newHead; p->next = newP->next;
p = p->next;
//要保证p不是空指针,不然调用p->next就会报错。
while(p){
newP->next = p->next;
newP = newP->next;
p->next = newP->next;
p = p->next;
}
return newHead;
}
};

【LeetCode练习题】Copy List with Random Pointer的更多相关文章

  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. C#两个时间的时间差的方法

    今天遇到一问题,计算两个时间的时间差,看网上的写法较为复杂,找到个简单点的,记录下作为自己的总结. 关键函数: DateTime.Subtract 函数解释: 从此实例中减去指定的日期和时间,返回一个 ...

  2. 探索PHP+Nginx(一) 安装Linux操作系统

    每次学习一种新的开发语言的时候,都要经历一个很纠结的过程,除非你运气很好或者准备工作充分,否则你在这个过程中总会耗费大量的时间和精力,当然你也会受益很多.而这个过程就是,开发环境的基础搭建,看似是装几 ...

  3. tr转换或删除字符

    字符处理命令:tr —— 转换或删除字符 逐个字符处理而不是处理单词的tr [OPTION]... SET1 [SET2]    -d: 删除出现在字符集中的所有字符 tr ab AB

  4. PHP 文件打开/读取

    PHP Open File - fopen() 打开文件的更好的方法是通过 fopen() 函数.此函数为您提供比 readfile() 函数更多的选项. 在课程中,我们将使用文本文件 "w ...

  5. MySQL数据库如何解决大数据量存储问题

    利用MySQL数据库如何解决大数据量存储问题? 各位高手您们好,我最近接手公司里一个比较棘手的问题,关于如何利用MySQL存储大数据量的问题,主要是数据库中的两张历史数据表,一张模拟量历史数据和一张开 ...

  6. mysql的wait_timeout配置

    mysql数据库有一个wait_timeout的配置,默认值为28800(即8小时). 在默认配置不改变的情况下,如果连续8小时内都没有访问数据库的操作,再次访问mysql数据库的时候,mysql数据 ...

  7. RehHat enterprise 5.4 安装git

    今天想来研究一下git,就自己安装一个试试,没想到遇到各种问题.经过各种百度和google,终于都解决了,现在来总结一下: 1.安装完redhat 5.4,安装gcc编译器的问题:这个gcc编译器需要 ...

  8. sql server数据库主键自增一插入特定值

    ID identity(1,1) SET IDENTITY_INSERT TableName ON INSERT TableName(ID) VALUES(110) SET IDENTITY_INSE ...

  9. union以及一些扩展

    select name,age from Students where Age<3unionselect name ,age from Students where Age >4--两个结 ...

  10. Intersection of Two Linked Lists(java)

    eg: a1 → a2 ↘ c1 → c2 → c3 或者直接a1 → b1 ↗ b1 → b2 → b3求公共链表c1. 常规的指针分裂法,复制法,偏移法. public class Solutio ...