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)相关推荐

  1. 【LeetCode】Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  2. LeetCode 138. Copy List with Random Pointer

    LeetCode 138. Copy List with Random Pointer 参考链接:http://www.cnblogs.com/grandyang/p/4261431.html Sol ...

  3. 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 ...

  4. LeetCode OJ - Copy List with Random Pointer

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  5. Leetcode 138 Copy List With Random Pointer

    题目描述 A linked list is given such that each node contains an additional random pointer which could po ...

  6. leetcode 138. Copy List with Random Pointer | 138. 复制带随机指针的链表(复杂链表的复制)

    题目 https://leetcode.com/problems/copy-list-with-random-pointer/ 题解 复杂链表的复制,经典问题,考察与 HashMap 的结合.注意如果 ...

  7. 【LeetCode】剑指 Offer 35. 复杂链表的复制

    [LeetCode]剑指 Offer 35. 复杂链表的复制 文章目录 [LeetCode]剑指 Offer 35. 复杂链表的复制 package offer;import java.util.Ar ...

  8. 【LeetCode】﹝并查集ி﹞连通分量个数(套用模板一直爽)

    [LeetCode]﹝并查集ி﹞连通分量个数(套用模板一直爽) 文章目录 [LeetCode]﹝并查集ி﹞连通分量个数(套用模板一直爽) 模板(使用路径压缩的加权quick-union算法) 连通网络 ...

  9. 【leetcode】解题日记(未完待续)

    开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...

  10. 【LeetCode】2022 7月 每日一题

    [LeetCode]2022 7月 每日一题 前言 七月太忙了,又是项目又是练车又是各种比赛.大概有10天的每日一题没有当天写完(虽然后面补上了). 将每日一题的所有思路记录在这里分享一下. 7.1 ...

最新文章

  1. 物联网概念升级,万物互联来袭
  2. 使用OpenCV进行人脸关键点检测
  3. 企业主要财务指标有哪些
  4. 【企业管理】《人与绩效》读书笔记--怎样做员工
  5. Bug貌似发现了centos系统配置host主机的问题
  6. IDEA注册jar包使用和常用插件
  7. 如何促合作共赢?技术人的一点经验分享
  8. 什么是端到端模型(end-to-end learning)?
  9. oracle的日志模式,Oracle数据日志模式
  10. 进程的一生@unix
  11. iOS开发值多线程简单介绍
  12. Spring Boot与Spring Cloud是什么关系?
  13. 超漂亮的CSS3按钮制作教程分享
  14. hivesql解析json格式的key与value
  15. Problem H. Curious (莫比乌斯反演)
  16. java计算机毕业设计消防网站源代码+数据库+系统+lw文档
  17. 【开源毕设】一款精美的家校互动APP分享——爱吖校推 [你关注的,我们才推](持续开源更新2)
  18. 动态设置高德地图中心点(坐标点),始终位于屏幕中心
  19. Oracle select表要带双引号的原因
  20. 设计数据库原则4个原则_四个设计原则

热门文章

  1. Postfix 邮件队列删除
  2. Android API 中文 (54) —— Filterable
  3. c语言各定义长度,关于C语言各个数据类型长度
  4. /proc/meminfo文件内容详解
  5. DPDK学习0 -- 学习步骤
  6. linux内核分析和应用 -- 进程与线程(上)
  7. Android P版本怎么简单的验证HIDL的Demo例程
  8. 蓝桥杯 k倍区间(前缀和)
  9. python sort sorted 排序详解
  10. 【图论】用匈牙利算法找女朋友(纯爱党的大胜利)