copy-list-with-random-pointer
2016-03-14 13:56
309 查看
题目:
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.
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.
/** * 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) { for (RandomListNode* cur = head; cur != NULL;) { RandomListNode* node = new RandomListNode(cur->label); node->next = cur->next; cur->next = node; cur = node-> next; } for (RandomListNode* cur = head; cur != NULL;) { if (cur->random != NULL) { cur->next->random = cur->random->next; } cur = cur->next->next; } // 拆链表 RandomListNode dummy(-1); for (RandomListNode* cur = head, *new_cur = &dummy; cur != NULL;) { new_cur->next = cur->next; new_cur = new_cur->next; cur->next = cur->next->next; cur = cur->next; } return dummy.next; } };
相关文章推荐
- CentOS 7磁盘分区及文件系统管理
- CentOS 7磁盘分区及文件系统管理
- 手动配置tomcat中的setclasspath
- 为Nginx 创建 Windows 的服务程序
- linux上安装配置vsftpd
- android中SELINUX规则分析和语法简介
- Vmware10安装centOS6.5图文教程
- 大并发服务器架构 && 大型网站架构演变
- linux下ssh登陆慢解决办法
- linux 命令行启动虚拟机
- 对反向传播算法(Back-Propagation)的推导与一点理解
- MyBatis异常-Property 'configLocation' not specified, using default MyBatis Configuration
- Ubuntu Linux 12.04 64位下Intel Math Kernel Library (MKL) 2013安装以及使用
- linux 命令
- Hadoop FS Shell命令大全
- tomcat类加载器-with源码
- linux-qt-qmake
- 163的yum源不再支持CentOS的6.6的版本
- 每个架构师都应该研究下康威定律
- strace,ltrace linux下跟踪进程调用的命令