复杂链表复制
来源:互联网 发布:linux 软阵列 编辑:程序博客网 时间:2024/05/16 19:24
#include <iostream>using namespace std;//复杂链表的复制struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) :label(x) ,next(NULL) ,random(NULL) {}};class Solution {public: RandomListNode* Clone(RandomListNode* pHead) { if(pHead==NULL) return NULL; //将链表每个结点都复制相同一个在其后连接:如1->2->3->NULL变为1->1->2->2->3->3->NULL RandomListNode* cur=pHead; while(cur) { RandomListNode* NewNode=new RandomListNode(cur->label); NewNode->next=cur->next; cur->next=NewNode; cur=cur->next->next; } //使复制的相同结点的随机指针指向前一个原结点随机指针下一个 cur=pHead; while(cur) { RandomListNode* Next=cur->next; if(cur->random) Next->random=cur->random->next; else Next->random=NULL; cur=cur->next->next; } //分解拆分链表成为两个 cur=pHead; RandomListNode* head2=cur->next; while(cur) { RandomListNode* Next=cur->next; cur->next=Next->next; if(cur->next) Next->next=cur->next->next; else Next->next=NULL; cur=cur->next; } return head2; }};
阅读全文
0 0
- 复制复杂链表
- 复制复杂链表
- 复杂链表复制
- 复制复杂链表
- 复制复杂链表
- 复制复杂链表
- 复杂链表复制
- 复杂链表复制
- 复杂链表复制
- 复杂链表复制
- 复杂链表复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- 复杂链表的复制
- spring data RedisCacheManager 源码查看与配置解析
- maven 上传解决The current branch is not configured for pull No value for key branch.master.merge found i
- activiti&flowable获取下一个审批节点
- b/s 和 c/s的区别
- 关于laravel的log组件使用
- 复杂链表复制
- 大一课程设计(TTMS单机版)
- 通过layer实现可输入的模态框
- Runtime的使用3 【字典转模型】
- python单元测试之unittest框架使用总结
- Android的高德地图实现行政区域显示
- tensorboard无法使用以及对应网址无法打开
- Java-json对象转Map
- CodeForces 106C Buns 多重背包