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. Android之自定义checkbox样式

    大部分情况下,我们在UI中并不采用Android自带的checkbox复选框样式,这时候就需要我们自定义自己的checkbox. 首先找两张checkbox背景图片,比如下图样子的: 然后在drawa ...

  2. UVA_Rotation Game<旋转游戏> UVA 1343

    The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The ...

  3. LeeCode-Number of 1 Bits

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has For exampl ...

  4. 电子科大POJ "3*3矩阵的乘法"

    3阶矩阵的乘法 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) c-source ...

  5. Redis 3.0集群 Window搭建方案

    Redis 3.0集群 Window搭建方案 1.集群安装前准备 安装Ruby环境,安装:rubyinstaller-2.3.0-x64.exe http://dl.bintray.com/onecl ...

  6. JDK5新特性之线程同步集合(五)

    一. 传统集合: 传统方式下的Collection在迭代集合时, 不同意对集合进行改动: public class CollectionModifyExceptionTest { public sta ...

  7. linux网络编程--跳水send和recv

    要了解一个概念:所有的TCP socket在内核具有发送缓冲器和接收缓冲器.TCP除了全双工操作模式TCP滑模取决于这两个单独buffer和这个buffer填充状态. 接收缓冲器数据缓存入内核.应用进 ...

  8. Java - 文件(IO流)

    Java - 文件 (IO)   流的分类:     > 文件流:FileInputStream | FileOutputStream | FileReader | FileWriter     ...

  9. <audio> 标签简介

    定义和用法 <audio> 标签定义声音,比如音乐或其他音频流. 实例 一段简单的 HTML 5 音频: <audio src="someaudio.wav"&g ...

  10. Intersection of Two Linked Lists(java)

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