类同:剑指 Offer 题目汇总索引第26题

Copy List with Random Pointer

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.

说明:分三步, 1. 每个节点复制其本身并链接在后面。 2, 复制随机指针。 3, 拆分链表。

/**
* 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) {
if(head == NULL) return NULL;
RandomListNode *head2, *p, *p2;
p = head2 = head;
while(p) {
RandomListNode *s = new RandomListNode(p->label);
s->next = p->next;
p->next = s;
p = s->next;
}
p = head;
while(p) {
if(p->random) p->next->random = p->random->next;
p = p->next->next;
}
head2 = p2 = head->next; p = head;
while(p) {
p->next = p->next->next;
p = p->next;
if(p2->next) {
p2->next = p2->next->next;
p2 = p2->next;
}
}
return head2;
}
};

16. Copy List with Random Pointer的更多相关文章

  1. 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表

    133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...

  2. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  3. Copy List with Random Pointer leetcode java

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  4. LintCode - Copy List with Random Pointer

    LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...

  5. [Leetcode Week17]Copy List with Random Pointer

    Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...

  6. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  7. LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)

    问题: A linked list is given such that each node contains an additional random pointer which could poi ...

  8. Leetcode Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  9. 【leetcode】Copy List with Random Pointer (hard)

    A linked list is given such that each node contains an additional random pointer which could point t ...

随机推荐

  1. servlet执行流程

    视频地址:http://www.imooc.com/video/5550      1-6 用户输入地址:localhost:8080/MyFirstServletDemo/index.jsp (My ...

  2. CentOS6.4安装Hadoop2.0.5 alpha - 3-Node Cluster

    1.在第2个个节点上重复http://www.cnblogs.com/littlesuccess/p/3361497.html文章中的第1-5步 2.修改第1个节点上的hdfs-site.xml中的配 ...

  3. Xceed Ultimate Suite Xceed界面控件套包下载

    Xceed Ultimate Suites是一款用户界面.数据处理套包,从.NET/WPF/silverLight平台到ActiveX下包含了65个子控件,以及Xceed公司的所有控件,具有表格.风格 ...

  4. win7下用python3.3获取cable modem的设备信息

    毕业一年多了,一直做cable modem的测试,总是觉得在国内这一行的人才很少,想找个师傅真的很不容易. 苦闷了许久之后,终于决定,自己去写点东西,万一就找到同行了呢? 下面就是本小姐写的第一篇博客 ...

  5. zlhome.com Deal

    using AnfleCrawler.Common; using System; using System.Collections.Generic; using System.Linq; using ...

  6. QT下调用摄像头(opencv2.4.4)

    http://www.cnblogs.com/yuliyang/p/3525107.html 项目pro文件: #------------------------------------------- ...

  7. Cfree

    #include<stdio.h>int main(){ printf("Hello World!!!/n"); return 0;} #include<stdi ...

  8. LintCode Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  9. toad 9.6和toad 12.1工具使用比较

    toad是我工作中使用最频繁的软件之一,阴错阳差的把2个版本都装到了电脑上,使用过程中逐渐发现2者的差异,特此做下记录,以便提示自己和其他有需要的人们.(随时更新中······)由于能力有限,难免会有 ...

  10. bootstrap-4

    html文档中,列表结构主要有三种:有序列表.无序列表和定义列表:<ul><li>.<ol><li>.<dl><dt><d ...