LeetCode138: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指针时,必须到两链表中进行查找。
这里,我采用了一些小技巧,当拷贝一个新节点时,将该新节点连接到原节点的后面
第一步做完后,遍历链表,设置拷贝节点的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的更多相关文章
- 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 ...
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 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 ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- 第35题:LeetCode138. Copy List with Random Pointer
题目 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深度拷贝. 考点 思路 代码 /** * Definition for singly ...
- [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 ...
随机推荐
- 第七章 二叉搜索树 (d1)AVL树:重平衡
- KO ------- 表中字段名和实体类属性名不一致
-----------------------siwuxie095 KO ------- 表中字段名和实体类属性名不一致 如果数据库表中的字段名和实体类的属性名不一致,那么在查询时, 相应字段的结果就 ...
- SVN概述
----------------------siwuxie095 SVN 概述 1.SVN 即 Subversion 的 ...
- 88. Merge Sorted Array 后插
合并两个排序的整数数组A和B变成一个新的数组.给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 假设A具有足够的空间(A数组的大小大于或等于m+n)去添加B ...
- 识别名人 · Find the Celebrity
[抄题]: 假设你和 n 个人在一个聚会中(标记为 0 到 n - 1),其中可能存在一个名人.名人的定义是所有其他 n - 1 人都认识他/她,但他/她不知道任何一个.现在你想要找出这个名人是谁或者 ...
- CentOS 7安装zabbix步骤
Zabbix配置安装 1.前期准备: 我自己的基础环境:CentOS 7 + Mysql 5.6 可以根据官网介绍一步一步安装,官网地址:https://www.zabbix.com/ 图1: 然后点 ...
- MVC仓储类Repository
接口: using Common; using System; using System.Collections; using System.Collections.Generic; using Sy ...
- jetty 8.0 add filter example
http://zyn010101.iteye.com/blog/1679798 package com.cicc.gaf.sso.server;import java.io.IOException;i ...
- part1:5Linux命令详解
1.Linux命令介绍 Linux命令是对Linux系统进行管理的命令.对于Linux系统来说,无论是中央处理器.内存.磁盘驱动器.键盘.鼠标还是用户等都是文件.Linux系统管理的命令是它正常运行的 ...
- IT 技术网站收集
## 脚本之家 http://www.jb51.net/ ## 菜鸟教程 http://www.runoob.com/ ## 编程中国 https://www.bccn.net/ ##