题目:

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. C语言实现24点程序

    一.简介 本程序的思想和算法来自于C语言教材后的实训项目,程序通过用户输入四个整数计算出能够通过加减乘除得到数字24的所有表达式,程序的设计有别于一般通过穷举实现的方式,效率得到提高.算法介绍如下: ...

  2. luoguP1080 国王游戏 (贪心+高精度)

    题目链接:https://www.luogu.org/problemnew/show/P1080 参考:https://www.luogu.org/problemnew/solution/P1080 ...

  3. Python str() 函数

    Python str() 函数  Python 内置函数 描述 str() 函数将对象转化为适于人阅读的形式. 语法 以下是 str() 方法的语法: class str(object='') 参数 ...

  4. 对实体类的CRUD操作

    --------------------siwuxie095 对实体类的 CRUD 操作 1.创建数据库和表 (1)创建一个 MySQL 连接:mybatis_conn (2)创建一个数据库:myba ...

  5. Java实现聚类算法k-means

    2016-07 java简单实现聚类算法 但是有一个小问题,,,,我其实每次迭代之后(就是达不到我的收敛标准之前,聚类中心的误差达不到指定小的时候),虽然重新算了聚类中心,但是其实我的那些点并没有变, ...

  6. 获取url路径中的参数

    简介 运用js的时候,我们有时可能会有这样的需求,就是想要获取浏览器地址栏指定的一项参数,形如:https://i.cnblogs.com/EditPosts.aspx?postid=8628413& ...

  7. Android无线调试_adbWireless

    NC的ADB驱动是个很让人头疼的问题,纵使老玩家有时候也是反复装装不上,有时候就算装上了,换一个ROM就又不行了,真是让人扣心扣肺,欲哭无泪,欲罢不能啊...现在好了,有了adbWireless不但可 ...

  8. python协程函数、递归、匿名函数与内置函数使用、模块与包

    目录: 协程函数(yield生成器用法二) 面向过程编程 递归 匿名函数与内置函数的使用 模块 包 常用标准模块之re(正则表达式) 一.协程函数(yield生成器用法二) 1.生成器的语句形式 a. ...

  9. FTP中各文件目录的说明

    DirectAdmin:FTP中各文件目录的说明     当您使用FTP连上空间后,FTP列表会出现以下文件和目录: domains目录:网站文件存放目录:public_html目录:快捷目录,可以快 ...

  10. MySQL之安装以及辅助工具的安装

    一 下载地址 MySQL 下载地址: http://rj.baidu.com/soft/detail/12585.html?ald 客户端工具:MavicatforMySQL 绿色版下载地址:http ...