36:Copy List with Random Pointer
来源:互联网 发布:android 全景拼接算法 编辑:程序博客网 时间:2024/06/11 07:35
本题解法代码的思想及编写参考了网址https://github.com/soulmachine/leetcode#leetcode题解
题目: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.
解题代码如下:
class Solution {public: RandomListNode* copyRandomList(RandomListNode* head) { for (RandomListNode* cur = head; cur != nullptr;) { RandomListNode* node = new RandomListNode(cur -> label); node -> next = cur -> next; cur -> next = node; cur = node -> next; } for (RandomListNode* cur = head; cur != nullptr; ) { if (cur -> random != nullptr) cur -> next -> random = cur -> random -> next; cur = cur -> next -> next; } // 分拆成两个单链表 RandomListNode dummy{-1}; for (RandomListNode *cur = head, *new_cur = &dummy; cur != nullptr; ) { new_cur -> next = cur -> next; new_cur = new_cur -> next; cur -> next = cur -> next -> next; cur = cur -> next; } return dummy.next; }};
0 0
- 36:Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- Copy List with Random Pointer
- 1086.Tree Traversals Again (25)...to be continued...
- IPC-Message-queue示例
- Spark Streaming中的操作函数分析
- Android面试题之Progress控件通过10秒的时间完成进度显示
- 机器学习中的特征工程
- 36:Copy List with Random Pointer
- HTTP长连接和短连接原理浅析
- STL容器之stack栈
- Java自定义异常类
- 欧氏-相似-仿射-射影几何
- 在二级上机题中发现conio.h
- 策略模式学习2
- WMPageController的使用
- 长链剖分随想