【leetcode】Copy List with Random Pointer (hard)
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位置,再拆分两个链表。
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; // Definition for singly-linked list with a random pointer.
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; //在每个结点后面复制一个自己 不管random指针
while(p != NULL)
{
RandomListNode * cpy = new RandomListNode(p->label);
cpy->next = p->next;
p->next = cpy; p = cpy->next;
} //复制random指针
p = head;
while(p != NULL)
{
RandomListNode * cpy = p->next;
if(p->random != NULL)
{
cpy->random = p->random->next;
} p = cpy->next;
} //把原链表与复制链表拆开
RandomListNode * cpyhead = head->next;
p = head;
RandomListNode * cpy = cpyhead;
while(p != NULL)
{
p->next = cpy->next;
cpy->next = (cpy->next == NULL) ? cpy->next : cpy->next->next; p = p->next;
cpy = cpy->next;
} return cpyhead;
}
}; int main()
{
RandomListNode * r = new RandomListNode(-);
Solution s;
RandomListNode * ans = s.copyRandomList(r); return ;
}
【leetcode】Copy List with Random Pointer (hard)的更多相关文章
- 【LeetCode】Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【Leetcode】【Hard】Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 【LEETCODE OJ】Copy List with Random Pointer
Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...
- Java for LeetCode 138 Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode] 138. Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- [LeetCode] 138. Copy List with Random Pointer 拷贝带随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- leetcode 138. Copy List with Random Pointer ----- java
A linked list is given such that each node contains an additional random pointer which could point t ...
随机推荐
- JavaWeb学习总结(五十一)——邮件的发送与接收原理
一. 邮件开发涉及到的一些基本概念 1.1.邮件服务器和电子邮箱 要在Internet上提供电子邮件功能,必须有专门的电子邮件服务器.例如现在Internet很多提供邮件服务的厂商:sina.sohu ...
- HDU 2010
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int Is_SXH(int num); int main() { int in1, ...
- SAS学习笔记<一>
三个周末的SAS课程宣告结束, 总结下来 第一周的统计原理简介 第二周/第三周讲解SAS的基本操作. 总体下来,对自己的知识结构有了一个新的梳理,对比大学时期,某个老师一上来就教我们SAS编程,而未考 ...
- Linux系统安装LAMP
说明: 系统版本:Ubuntu14.04-LTS,可以在Ubuntu官网直接下载.Ubuntu其他版本也可安装本方法搭建LAMP环境! 步骤一,安装apache2 1 sudo apt-get ins ...
- PHP简易聊天室&调试问题
在进入login.php程序之后 <?php error_reporting(E_ALL^E_NOTICE); session_start(); //装载Session库,一定要放在首行 $u ...
- AMD正式公布第七代桌面级APU AM4新接口
导读 本月5日,AMD正式公布了入门级的第七代桌面级APU为Bristol Ridge,在性能和能效方面较上一代产品拥有显著提升.AMD同时确认Zen处理器和新APU(Bristol Ridge)都将 ...
- 跟着百度学PHP[4]OOP面对对象编程-16-switch逻辑就语句
直接看案例.较为简单.不解释. <?php $chengji="; #先定义一个变量赋值一个数值 switch ($chengji) { && $chengji > ...
- 跟着百度学PHP[4]OOP面对对象编程-15-魔术方法__call方法
简而言之就是调用了一个类中没有的方法就会自动调用__call()方法, 该参数有两个必须的参数! 第一个参数:调用的不存在的方法的方法名. 第二个参数:调用不存在的方法的参数. 但是总的说回来,__c ...
- Unity3D获取Andorid设备返回键,主页键等功能
在Unity开发中捕捉Android的常用事件其实很简单 在新建的脚本文件中就加入: 比如: // 返回键 if ( Application.platform == RuntimePlatform.A ...
- Triangle
动态规划 int minimumTotal (vector<vector<int>>& triangle) { ; i >= ; --i) ; j < i ...