【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(-1);Solution s;RandomListNode * ans = s.copyRandomList(r);return 0; }
【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 138. Copy List with Random Pointer
LeetCode 138. Copy List with Random Pointer 参考链接:http://www.cnblogs.com/grandyang/p/4261431.html Sol ...
- leetcode -day8 Copy List with Random Pointer Single Number I II
五一中间断了几天,开始继续... 1. Copy List with Random Pointer A linked list is given such that each node cont ...
- LeetCode OJ - Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- Leetcode 138 Copy List With Random Pointer
题目描述 A linked list is given such that each node contains an additional random pointer which could po ...
- leetcode 138. Copy List with Random Pointer | 138. 复制带随机指针的链表(复杂链表的复制)
题目 https://leetcode.com/problems/copy-list-with-random-pointer/ 题解 复杂链表的复制,经典问题,考察与 HashMap 的结合.注意如果 ...
- 【LeetCode】剑指 Offer 35. 复杂链表的复制
[LeetCode]剑指 Offer 35. 复杂链表的复制 文章目录 [LeetCode]剑指 Offer 35. 复杂链表的复制 package offer;import java.util.Ar ...
- 【LeetCode】﹝并查集ி﹞连通分量个数(套用模板一直爽)
[LeetCode]﹝并查集ி﹞连通分量个数(套用模板一直爽) 文章目录 [LeetCode]﹝并查集ி﹞连通分量个数(套用模板一直爽) 模板(使用路径压缩的加权quick-union算法) 连通网络 ...
- 【leetcode】解题日记(未完待续)
开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...
- 【LeetCode】2022 7月 每日一题
[LeetCode]2022 7月 每日一题 前言 七月太忙了,又是项目又是练车又是各种比赛.大概有10天的每日一题没有当天写完(虽然后面补上了). 将每日一题的所有思路记录在这里分享一下. 7.1 ...
最新文章
- 物联网概念升级,万物互联来袭
- 使用OpenCV进行人脸关键点检测
- 企业主要财务指标有哪些
- 【企业管理】《人与绩效》读书笔记--怎样做员工
- Bug貌似发现了centos系统配置host主机的问题
- IDEA注册jar包使用和常用插件
- 如何促合作共赢?技术人的一点经验分享
- 什么是端到端模型(end-to-end learning)?
- oracle的日志模式,Oracle数据日志模式
- 进程的一生@unix
- iOS开发值多线程简单介绍
- Spring Boot与Spring Cloud是什么关系?
- 超漂亮的CSS3按钮制作教程分享
- hivesql解析json格式的key与value
- Problem H. Curious (莫比乌斯反演)
- java计算机毕业设计消防网站源代码+数据库+系统+lw文档
- 【开源毕设】一款精美的家校互动APP分享——爱吖校推 [你关注的,我们才推](持续开源更新2)
- 动态设置高德地图中心点(坐标点),始终位于屏幕中心
- Oracle select表要带双引号的原因
- 设计数据库原则4个原则_四个设计原则