Leetcode 138. Copy List with Random Pointer
来源:互联网 发布:如何购买已备案域名 编辑:程序博客网 时间:2024/04/27 08:41
#include <iostream>using namespace std;struct RandomListNode { int label; RandomListNode *next, *random; RandomListNode(int x) : label(x), next(NULL), random(NULL) {}};RandomListNode* Clone(RandomListNode* pHead) { RandomListNode* node=pHead; while (node) { RandomListNode* tmp=new RandomListNode(node->label); tmp->next=node->next; node->next=tmp; node=tmp->next; } node=pHead; while (node) { node->next->random=node->random ? node->random->next : nullptr; node=node->next->next; } node=pHead;RandomListNode preHead(0),*p=&preHead; while (node) { p->next=node->next; p=p->next; node->next=node->next->next; //避免破坏原结构 node=node->next; } return preHead.next; }//进行测试int main(){ RandomListNode *p1 = new RandomListNode(1); RandomListNode *p2 = new RandomListNode(2); RandomListNode *p3 = new RandomListNode(3); p1->next = p2; p2->next = p3; p3->next = nullptr; p1->random = p3; p2->random = nullptr; p3->random = p2; RandomListNode *newlink; newlink = copyRandomList(p1); while (newlink != nullptr) { cout << newlink->label << endl; newlink = newlink->random; }}
0 0
- LeetCode 138. Copy List with Random Pointer
- [Leetcode] 138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- [leetcode] 138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- leetcode.138. Copy List with Random Pointer
- LeetCode-138.Copy List with Random Pointer
- Leetcode 138. Copy List with Random Pointer
- [Leetcode]138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- [LeetCode] 138. Copy List with Random Pointer
- [LeetCode]138. Copy List with Random Pointer
- LeetCode 138. Copy List with Random Pointer
- Leetcode-138. Copy List with Random Pointer
- Leetcode 138. Copy List with Random Pointer
- leetcode 138. Copy List with Random Pointer
- Leetcode 138. Copy List with Random Pointer
- 编写php扩展的问题
- Python 面试题(下)
- 20个经典管理学定律
- centos7设备命名
- Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
- Leetcode 138. Copy List with Random Pointer
- CentOS7安装openjdk、tomcat和mysql流程介绍
- 面试题
- SQL注入攻防入门详解
- 如何在Linux下编译Cocos2dx3.11项目(主要是添加源文件喇~)
- Mybatis步步进阶(四)——Mybatis框架流程及核心配置详解
- 一文读懂卷积神经网络
- Longest Chain
- 正则表达式多行修饰符m的问题