输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

第一种方法:使用map存放原节点和其复制节点。然后再给复制节点的next/random指针初始化。初始化时,原节点p,p的复制节点的next=p.next的复制节点

见代码:

    public RandomListNode copyRandomList(RandomListNode head) {
if(head==null) return null;
//map存放原节点和复制节点
Map<RandomListNode,RandomListNode> map=new HashMap<RandomListNode,RandomListNode>();
RandomListNode p=head;
//将所有节点及其复制节点放到hashmap中
while(p!=null){
map.put(p,new RandomListNode(p.label));
p=p.next;
}
//给复制节点的指针赋值
p=head;
while(p!=null){
map.get(p).next=map.get(p.next);//p的复制节点的next等于p.next的复制节点。
map.get(p).random=map.get(p.random);
p=p.next;
}
return map.get(head);
}

第二种方法:

分三步:
1、根据next指针,复制原链表,在原链表中复制,将所有节点复制到原节点后面(如a->b->c复制成a->a'->b->b'->c->c')
2、根据链表的random指针,在新链表中初始化复制节点的random指针。.next.random=.random.next
3、从复制后的链表中拆分出新的复制链表返回
如果只有一个next指针,则省略上面第2步

public RandomListNode copyRandomList(RandomListNode head) {
if(head==null) return null;
RandomListNode pCur=head;//用于遍历
//第一步,根据next指针复制如a->b->c复制成a->a'->b->b'->c->c'
while(pCur!=null){
RandomListNode node=new RandomListNode(pCur.label);
node.next=pCur.next;
pCur.next=node;
pCur=node.next;
} pCur=head;
//第二步,初始化复制后节点的random指针
while(pCur!=null){
if(pCur.random!=null){ //不用判断p.next是否为空,因为p不为空,p.next是复制节点,肯定不为空
pCur.next.random=pCur.random.next;//要指向p.random的复制节点,所以有.next }
pCur=pCur.next.next;
} //第三步,拆分
RandomListNode phead=head.next;
RandomListNode p=phead;
pCur=head;
while(pCur!=null){
pCur.next=pCur.next.next;
if(p.next!=null){
p.next=p.next.next;
}
p=p.next;
pCur=pCur.next;
} return phead;
}

可以看出,上面第一种方法简单很多。

Copy List with Random Pointer(复杂链表复制)的更多相关文章

  1. leetcode 138. Copy List with Random Pointer复杂链表的复制

    python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...

  2. 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 ...

  3. 16. Copy List with Random Pointer

    类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...

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

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

  5. Copy List with Random Pointer leetcode java

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

  6. [Leetcode Week17]Copy List with Random Pointer

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

  7. LintCode - Copy List with Random Pointer

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

  8. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  9. LeetCode OJ:Copy List with Random Pointer(复制存在随机链接的链表)

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

随机推荐

  1. python 子包引用父包和其他子包

    python 子包引用父包和其他子包 python引用子目录很简单, 里面放个__init__.py就可以了. 如何在子目录里面引用其他目录(父目录,爷目录和同辈分目录)呢? 例如: python有项 ...

  2. linux下可执行文件的库们

    在Linux下有一些命令可以让我们知道可执行文件的很多信息. 记录如下: ldd : print shared library dependencies nm: list symbols from o ...

  3. Android的GridView的用法-android学习之旅(二十七)

    Gridview简介 GridView和ListView有相同的父类AbsListView.他和ListView唯一的区别是Gridview可以显示多列,如果不设置列数,就默认显示一列,变成了List ...

  4. J2EE学习从菜鸟变大鸟之五 JDBC(Java Data Base Connectivity)

    JDBC刚开始学习这个的时候看到了,以为是ODBC呢,很是相似啊,总的来说还是基本上一类的东东,但是还有一些细微的区别,下面和大家一起分享学习. JDBC(Java Data Base Connect ...

  5. RabbitMQ消息队列(六):使用主题进行消息分发

    在上篇文章RabbitMQ消息队列(五):Routing 消息路由 中,我们实现了一个简单的日志系统.Consumer可以监听不同severity的log.但是,这也是它之所以叫做简单日志系统的原因, ...

  6. 海量数据挖掘MMDS week4: 推荐系统Recommendation System

    http://blog.csdn.net/pipisorry/article/details/49205589 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...

  7. Jquery之Bind方法参数传递与接收的三种方法

     方法一. function GetCode(event) { alert(event.data.foo); } $(document).ready(function() { $("#s ...

  8. 监控系统的多协议直播(RTSP RTMP HTTP Live Streaming)

    监控系统的多协议直播(RTSP RTMP  HTTP Live Streaming)

  9. Spring获取bean的步骤

    1 处理&问题 2 去chache里找 3 处理对象A依赖对象B的问题 4 生成bean   4.1 处理方法注入 ------lookup-method   4.2 如果类实现了Instan ...

  10. android加载大图,防止oom

    高效加载大图片 我们在编写Android程序的时候经常要用到许多图片,不同图片总是会有不同的形状.不同的大小,但在大多数情况下,这些图片都会大于我们程序所需要的大小.比如说系统图片库里展示的图片大都是 ...