链表——复杂链表的复制
2016-05-09 12:36
357 查看
题目:
在复杂链表中,每个节点除了一个next引用外还有一个random引用指向链表中的任意结点或者null,实现函数复制该链表。
方法一:HashMap存储结点对信息
第一步:(遍历原链表一次)不考虑random引用,将原链表作为单链表进行复制,并用HashMap将结点的配对信息存储;
第二步:(第二次遍历原链表)设置复制链表上的每个random结点;
总的时间复杂度为O(N),空间复杂度为O(N)。
代码如下:
方法二:不使用辅助空间,时间复杂度为O(N)
第一步:复制原始链表每个结点N并创建新节点N',将N‘链接到N的后面;
第二步:设置复制出来的结点N'的random引用,如果原链表结点N的random指向S,则N'的random引用指向S下一个结点S';
第三部:把这个长链表拆分成两个链表。
public class Solution {
public RandomListNode Clone(RandomListNode head)
{
if(head == null)
return head;
copyList(head);
copyRandom(head);
return devideList(head);
}
//1.复制节点
public void copyList(RandomListNode head)
{
RandomListNode cur=head;
while(cur!=null)
{
RandomListNode node=new RandomListNode(cur.label);
node.next=cur.next;
cur.next=node;
cur=node.next;
}
}
//2.复制random指针
public void copyRandom(RandomListNode head)
{
RandomListNode cur=head;
while(cur!=null)
{
RandomListNode node=cur.next;
if(cur.random!=null)
node.random=cur.random.next;
cur=cur.next.next;
}
}
//3.拆分链表
public RandomListNode devideList(RandomListNode head)
{
RandomListNode cur=head;
RandomListNode phead=head.next;
while(cur.next!=null&&cur.next.next!=null)
{
RandomListNode node=cur.next;
cur.next=node.next;
cur=cur.next;
node.next=cur.next;
node=node.next;
}
cur.next=null;
return phead;
}
}
在复杂链表中,每个节点除了一个next引用外还有一个random引用指向链表中的任意结点或者null,实现函数复制该链表。
方法一:HashMap存储结点对信息
第一步:(遍历原链表一次)不考虑random引用,将原链表作为单链表进行复制,并用HashMap将结点的配对信息存储;
第二步:(第二次遍历原链表)设置复制链表上的每个random结点;
总的时间复杂度为O(N),空间复杂度为O(N)。
代码如下:
/** * Definition for singly-linked list with a random pointer. * class RandomListNode { * int label; * RandomListNode next, random; * RandomListNode(int x) { this.label = x; } * }; */ import java.util.*; public class Solution { public RandomListNode copyRandomList(RandomListNode head) { if(head==null) return null; //构建一个哈希表 HashMap<RandomListNode,RandomListNode> map=new HashMap(); RandomListNode p=head; RandomListNode ptemphead=new RandomListNode(0);//创建一个辅助头结点; RandomListNode ptemp=ptemphead; //对除了random引用外的单链表进行复制,并将结点成对存储在map中; while(p!=null) { RandomListNode node=new RandomListNode(p.label); map.put(p,node); ptemp.next=node; ptemp=ptemp.next; p=p.next; } ptemp.next=null; //第二次遍历原链表,复制random引用; p=head; ptemp=ptemphead.next; while(p!=null) { if(p.random!=null) { ptemp.random=map.get(p.random); } else { 4000 ptemp.random=null; } p=p.next; ptemp=ptemp.next; } return ptemphead.next; } }
方法二:不使用辅助空间,时间复杂度为O(N)
第一步:复制原始链表每个结点N并创建新节点N',将N‘链接到N的后面;
第二步:设置复制出来的结点N'的random引用,如果原链表结点N的random指向S,则N'的random引用指向S下一个结点S';
第三部:把这个长链表拆分成两个链表。
public class Solution {
public RandomListNode Clone(RandomListNode head)
{
if(head == null)
return head;
copyList(head);
copyRandom(head);
return devideList(head);
}
//1.复制节点
public void copyList(RandomListNode head)
{
RandomListNode cur=head;
while(cur!=null)
{
RandomListNode node=new RandomListNode(cur.label);
node.next=cur.next;
cur.next=node;
cur=node.next;
}
}
//2.复制random指针
public void copyRandom(RandomListNode head)
{
RandomListNode cur=head;
while(cur!=null)
{
RandomListNode node=cur.next;
if(cur.random!=null)
node.random=cur.random.next;
cur=cur.next.next;
}
}
//3.拆分链表
public RandomListNode devideList(RandomListNode head)
{
RandomListNode cur=head;
RandomListNode phead=head.next;
while(cur.next!=null&&cur.next.next!=null)
{
RandomListNode node=cur.next;
cur.next=node.next;
cur=cur.next;
node.next=cur.next;
node=node.next;
}
cur.next=null;
return phead;
}
}
相关文章推荐
- using关键字的用法
- B - Game of Robots
- 最近笔记
- linux复制远程文件和文件夹
- Java基础7
- using关键字背后的故事!
- 操作系统——进程与线程
- 流媒体开发(二)后台音频播放
- Linux基础知识
- 求2000以内的素数
- 第十周项目1(1)
- 成员函数、友元函数和一般函数有区别-两点间的距离
- 关于using关键字
- Codeforces Round #344 (Div. 2) C. Report
- 图论-最短路-floyd
- 摘]【PL/SQL Developer连接MS SQL Server的方法】
- js--String对象方法属性整理
- ssh是sessionFactory为null
- bzoj4034 T2 【树链剖分+线段树】
- MySql数据库查询多级部门及其下的所有用户信息