复制含有随机指针节点的链表

【题目】 一种特殊的链表节点类描述如下: public class Node { public int value; public Node next; public Node rand; public Node(int data) { this.value = data; } } Node类中的value是节点值,next指针和正常单链表中next指针的意义 一 样,都指向下一个节点,rand指针是Node类中新增的指针,这个指 针可 能指向链表中的任意一个节点,也可能指向null。 给定一个由 Node节点类型组成的无环单链表的头节点head,请实现一个 函数完成 这个链表中所有结构的复制,并返回复制的新链表的头节点。
进阶: 不使用额外的数据结构,只用有限几个变量,且在时间复杂度为 O(N) 内完成原问题要实现的函数。
核心代码:

package basic_class_03;

import java.util.HashMap;

/**
 * 类名:Code_13_CopyListWithRandom<br>
 * 功能:复制含有随机指针节点的链表<br>
 * 作者:java战士<br>
 * 日期:2019/9/4<br>
 * 版本:v1.0.0
 * 历史修订:
 */
public class Code_13_CopyListWithRandom {

    public static class Node{
        public int data;
        public Node next;
        public Node rand;

        public Node(int data) {
            this.data = data;
        }
    }
    //借用HashMap作为辅助空间,进行拷贝,空间复杂度为O()
    public static Node copyLinkList1(Node head){
        HashMap<Node,Node> map=new HashMap<Node,Node>();
        Node cur=head;
        while (cur!=null){
            map.put(cur,new Node(cur.data));
            cur=cur.next;
        }
        cur=head;
        while (cur!=null){
            map.get(cur).next=map.get(cur.next);
            map.get(cur).rand=map.get(cur.rand);
            cur=cur.next;
        }
        return map.get(head);
    }
    //不适用辅助空间,时间复杂度为O(N),额外空间复杂度为O(1)
    public static Node copyLinkedList2(Node  head){
        if (head==null){
            return null;
        }
        Node cur=head;
        Node next=null;
        //拷贝整个链表
        while (cur!=null){
            next=cur.next;
            cur.next=new Node(cur.data);
            cur.next.next=next;
            cur=next;
        }
        cur=head;
        Node copyCur=null;
        //拷贝链表自由指针
        while (cur!=null){
            next=cur.next.next;
            copyCur=cur.next;
            copyCur.rand=cur.rand!=null?cur.rand.next:null;
            cur=next;
        }
        Node res=head.next;
        cur=head;
        while (cur!=null){
            next=cur.next.next;
            copyCur=cur.next;
            cur.next=next;
            copyCur.next=next!=null?next.next:null;
            cur=next;
        }
        return res;
    }
    public  static void printRandLinkedList(Node head){
        Node cur=head;
        System.out.print("order:");
        while (cur!=null){
            System.out.print(cur.data+" ");
            cur=cur.next;
        }
        System.out.println();
        cur=head;
        System.out.print("rand:");
        while ( cur!=null){
            System.out.print(cur.rand==null?"- ":cur.rand.data+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    public static void main(String[] args){
        Node head=new Node(1);
        head.next=new Node(2);
        head.next.next=new Node(3);
        head.next.next.next=new Node(4);
        head.next.next.next.next=new Node(5);
        head.next.next.next.next.next=new Node(6);

        head.rand=head.next.next.next.next.next;//1---------6
        head.next.rand=head.next.next.next.next;//2---------5
        head.next.next.rand=head.next.next.next;//3---------4
        head.next.next.next.rand=head.next;//4--------2
        head.next.next.next.next.rand=head.next;//5------------2
        head.next.next.next.next.next.rand=head;//6------------1

        printRandLinkedList(head);
        //Node res=copyLinkList1(head);
        Node res=copyLinkedList2(head);
        printRandLinkedList(res);
    }
}

测试结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值