[leetcode] Copy List with Random Pointer
来源:互联网 发布:mac音量键在哪里设置 编辑:程序博客网 时间:2024/06/07 01:56
Copy List with Random Pointer
先在原来的链表上复制节点,然后拆分。
/** * 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) { RandomListNode *cur=head; while(cur!=nullptr){ RandomListNode *node=new RandomListNode(cur->label);//新建节点 node->next=cur->next; cur->next=node; cur=node->next; }//N1M1N2M2... cur=head; while(cur!=nullptr){ if(cur->random!=NULL){ cur->next->random=cur->random->next;//拷贝random } cur=cur->next->next; } //分拆两个单链表 cur=head; RandomListNode *new_Cur=new RandomListNode(0); RandomListNode *new_List=new_Cur; while(cur!=nullptr){ new_Cur->next=cur->next; cur->next=cur->next->next; cur=cur->next; new_Cur=new_Cur->next; } return new_List->next; }};
0 0
- [leetcode][list] Copy List with Random Pointer
- Leetcode: Copy List with Random Pointer
- LeetCode: Copy List with Random Pointer
- [LeetCode] Copy List with Random Pointer
- [leetcode]Copy List with Random Pointer
- LeetCode:Copy List with Random Pointer
- leetcode Copy List with Random Pointer
- LeetCode:Copy List with Random Pointer
- LeetCode题解:Copy List with Random Pointer
- [LeetCode]Copy List with Random Pointer
- leetcode Copy List with Random Pointer
- leetcode-Copy List with Random Pointer
- Leetcode: Copy List with Random Pointer
- LeetCode | Copy List with Random Pointer
- [LeetCode] Copy List with Random Pointer
- LeetCode OJ:Copy List with Random Pointer
- LeetCode(138)Copy List with Random Pointer
- [LeetCode] - Copy List with Random Pointer
- 校赛4
- 对数据库表中的某一字段去重分组排序
- 关于我的博客的内容
- 浅谈C语言中的联合体
- [Django]Django的orm中get和filter的不同
- [leetcode] Copy List with Random Pointer
- HDU4952
- DSM2 DSMX Satelite 进入 Bind 模式
- mac os x 如何打开多个终端
- 大端和小端及如何判断
- [leetcode] Candy
- 硬盘中出现eula.1028.txt等垃圾文件的原因及是否可删
- poj 1657 Distance on Chessboard
- [leetcode] Gas Station