题目:

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. 禁止直接访问ashx页面

      if (context.Request.ServerVariables["HTTP_REFERER"] == null)             {               ...

  2. for循环计算阶乘

    int x = 10; for(int y = x - 1; y >= 1; y--) { x = x * y; } System.out.println(x); 从10乘到1的阶乘写法. lo ...

  3. Web开发: servlet的session为null?

    servlet的session(会话)显示为null,一般是web.xml中配置不对或者在浏览器输入的url不正确造成的. web.xml配置如下: <servlet> <servl ...

  4. 使用Navicat Premium连接mysql数据库

    Navicat Premium是一个可多重连接的数据库管理工 具,它可让你以单一程序同时连接. Navicat Premium 使你能简单并快速地在各种数据库系统间传输数据,或传输一份指定 SQL 格 ...

  5. Clustered Index & Non Clustered Index(聚簇索引和非聚簇索引)

    每个表只能有一个聚簇索引,而能有200多个非聚簇索引. 在物理分配上, 每个表的数据都是分配在页上,一个页大概有8k左右,假设一条数据占1000字节的话,那么8000条数据占8000*1k/8k = ...

  6. .NET4.0的listview与DataPager的结合使用时的模板编辑

    1.设置listview模板样式: <asp:ListView ID="ListView1" runat="server" DataSourceID=&q ...

  7. 【校招面试 之 C/C++】第26题 C++ 智能指针(二)之 share_ptr

    1.综述 shared_ptr 是一个标准的共享所有权的智能指针, 允许多个指针指向同一个对象. 定义在 memory 文件中(非memory.h), 命名空间为 std. shared_ptr 是为 ...

  8. Java中spring读取配置文件的几种方法

    Spring读取配置XML文件分三步: 一.新建一个Java Bean: package springdemo; public class HelloBean { private String hel ...

  9. 如何在64位WIN7旗舰版下安装SQL2000

    1>找到安装包下面的“DEVELOPER”或“ENTERPRISE”等下的X86\SETUP下的“SETUPSQL.EXE”,在安装前右键单击这个文 件, 1.1 打开“兼容性”标签,兼容模式选 ...

  10. sqlserver查询区分大小写

    例子: select * from tb_students where name='jay' select * from tb_students where name='JAY' 这两句查询结果是一样 ...