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 ...
随机推荐
- 调试PHP如何让浏览器提示错误
php.ini中的display_errors的值改为On:或者php代码页顶部加上ini_set("display_errors", "On"); error ...
- The partial charge density (1)
============================================================================================= The pa ...
- ASP.NET中修改从数据库获取的datatable中的值
有些时候,我们从数据库表中获取一个实体的对象,但有些内容并不是最终显示的内容,格式也都是不一样.经过一番尝试,发现datatable中的数值如果跟想要改变的类型不一致,就无法更改,只有添加新列,然后把 ...
- manacher最长回文序列c++
算法真心读不懂 #include <iostream>#include<string>#include<cstring> using namespace std;c ...
- iOS 基于MVC设计模式的基类设计
iOS 基于MVC设计模式的基类设计 https://www.jianshu.com/p/3b580ffdae00
- [leetcode]543. Diameter of Binary Tree二叉树直径
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- vi编辑时出现E325:ATTENTION
我们用vi编辑文件时,系统会提示E325:ATTENTION. 这是由于在编辑该文件的时候异常退出了,因为vi在编辑文件时会创建一个交换文件swap file以保证文件的安全性. 但是每次打开文件时都 ...
- css布局---各种居中
居中是我们使用css来布局时常遇到的情况.使用css来进行居中时,有时一个属性就能搞定,有时则需要一定的技巧才能兼容到所有浏览器,本文就居中的一些常用方法做个简单的介绍. 注:本文所讲方法除了特别说明 ...
- Ubuntu12.04添加环境变量
环境变量分为系统级和用户级. 系统级变量设置环境为/etc/environment和/etc/profile等,不要轻易修改,否则可能造成系统错误. 用户级变量设置路径为-/.bashrc和~/.pr ...
- 2018.10.23 vijo1243生产产品(单调队列优化dp)
传送门 这道单调队列真的有点难写啊. 方程感觉挺简单的. f[i][j]f[i][j]f[i][j]表示在第iii个车间结束前jjj次步骤的最小代价. 然后用单调队列毒瘤优化一下就行了. 代码: #i ...