题目:

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指针时,必须到两链表中进行查找。

这里,我采用了一些小技巧,当拷贝一个新节点时,将该新节点连接到原节点的后面

第一步做完后,遍历链表,设置拷贝节点的random指针,设置拷贝节点的random指针时,可根据原节点的random指针进行设置,因为原节点的random指向的节点的下一个节点即为拷贝节点额random要指向的节点。

最后,将链表进行分离即可。

实现代码:

#include <iostream>
using namespace std; struct RandomListNode
{
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
}; class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if(head == NULL)
return NULL;
RandomListNode *p = head;
while(p)
{
RandomListNode *node = new RandomListNode(p->label);//拷贝一个新节点,然后将该新节点链接到原节点的后面
node->next = p->next;
p->next = node;
p = node->next;
} p = head;
while(p)//根据原节点设置新节点的random指针
{
if(p->random)//如果原节点的random指针不为空则设置拷贝节点
{
//拷贝节点的random指针指向的节点可利用原节点的random指针找到,
//因为每个拷贝节点都在原节点的下一个节点
p->next->random = p->random->next;
} p = p->next->next;
} //将原链表和新建链表进行分离
RandomListNode *chead = head->next;
head->next = head->next->next;
RandomListNode *q = chead;
head = head->next;
while(head)
{
q->next = head->next;
head->next = head->next->next;
head = head->next;
q = q->next; }
return chead; }
};
int main(void)
{
return ;
}

LeetCode138:Copy List with Random Pointer的更多相关文章

  1. LeetCode OJ:Copy List with Random Pointer(复制存在随机链接的链表)

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

  2. 16. Copy List with Random Pointer

    类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...

  3. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  4. Copy List with Random Pointer leetcode java

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

  5. [Leetcode Week17]Copy List with Random Pointer

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

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

  7. LintCode - Copy List with Random Pointer

    LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...

  8. 第35题:LeetCode138. Copy List with Random Pointer

    题目 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深度拷贝. 考点 思路 代码 /** * Definition for singly ...

  9. [Java]LeetCode138. 复制带随机指针的链表 | Copy List with Random Pointer

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

随机推荐

  1. richface的配置、用法介绍和注意事项

    richface的配置.用法介绍和注意事项一.RichFaces (3.1.x) 技术需求 1.JDK 1.5 或更高版本: 2.支持的 JSF 实现: Sun JSF 1.1 RI - 1.2 My ...

  2. oracle中的分支与循环语句

    分支语句 if的三种写法一, if 2 < 1 then dbms_output.put_line('条件成立'); end if; 二, if 2 < 1 then dbms_outpu ...

  3. The Unique MST

    The Unique MST http://poj.org/problem?id=1679 Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  4. nginx 反向代理 apache 服务

    反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时 ...

  5. Apache Cordova vs Adobe PhoneGap: the differences and which one to use

    http://www.makehybridapps.com/2014/06/09/cordova-vs-phonegap-the-differences-and-which-one-to-use/

  6. 第三章 RNA测序

    第三章 RNA测序   RNA测序(RNA Sequencing,简称RNA-Seq,也被称为全转录物组鸟枪法测序Whole Transcriptome Shotgun Sequencing,简称WT ...

  7. select查询

    4.2  查询功能 SQL的核心是查询.SQL的查询命令也称作SELECT命令,它的基本形式由SELECT-FROM-WHERE查询块组成,多个查询块可以嵌套执行. 以下表为以后的例子中使用表: 档案 ...

  8. 测试用例excel模板

    Index Precondition Action Test Data Expect Result Excute Date Pass/Fail

  9. css之颜色篇

      app多采用浅灰#f5f5f5   白色一般用white,如果觉得白太直接了,可以加一点点灰,#fefefe,   这种情况下搭配#e4e4e4的浅灰边框最合适.

  10. 如何使用css来让图片居中不变形 微信小程序和web端适用

    图片变形很多人祭奠出了妖魔鬼怪般的各种大法,比如使用jq来写,或者使用css表达式来写.今天我总结的是使用css3来写,唯一最大缺点就是对一些浏览器版本不够兼容.下面就是关于如何使用css来让图片居中 ...