数据结构与算法(Java) 30:复制含有随机指针节点的链表

题目 一种特殊的链表节点类描述如下:

public static class Node{
        public int value;
        public Node next;
        public Node random;

        public Node(int value){
            this.value = value;
        }
    }

Node类中的value是节点值,next指针和正常单链表中next指针的意义一 样,都指向下一个节点,random指针是Node类中新增的指针,这个指针可能指向链表中的任意一个节点,也可能指向null。 给定一个由 Node节点类型组成的无环单链表的头节点head,请实现一个函数完成这个链表中所有结构的复制,并返回复制的新链表的头节点。

进阶 不使用额外的数据结构,只用有限几个变量,且在时间复杂度为 O(N) 内完成原问题要实现的函数。

思路 (1)使用哈希表实现。时间复杂度为O(1),增删改查时间复杂度都是O(1)。哈希表是K-V结构,即key,value。首先遍历一遍链表,将原链表的节点作为key,该节点的复制版作为key值对应的value,且此时value对应的复制版节点只存放了node.value值,而不知道node.next以及node.random是什么。第二次遍历链表,借用哈希表的key值完善value中节点的node.next以及node.random指针。

       (2)【进阶】在原链表上进行操作。首先遍历一次链表,在遍历的同时,将每个节点复制版本插入到链表中对应节点的后面,即A\rightarrow B\rightarrow C 变为A\rightarrow {A}'\rightarrow B\rightarrow {B}'\rightarrow C\rightarrow {C}'。第二遍遍历,补充复制版节点的node.random值。第三遍遍历,将链表分开,即A\rightarrow {A}'\rightarrow B\rightarrow {B}'\rightarrow C\rightarrow {C}' 变为A\rightarrow B\rightarrow C 和 {A}'\rightarrow {B}'\rightarrow {C}',返回{A}'\rightarrow {B}'\rightarrow {C}'即可。

package algorithm.section4;

import java.util.HashMap;

public class CopyListWithRandom {

    public static class Node{
        public int value;
        public Node next;
        public Node random;

        public Node(int value){
            this.value = value;
        }
    }

    public static Node method1(Node head){
        if (head == null) return null;

        Node cur = head;
        HashMap<Node, Node> map = new HashMap<>();

        while (cur != null){
            map.put(cur, new Node(cur.value));
            cur = cur.next;
        }

        cur = head;

        while (cur != null){
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }

        return map.get(head);
    }

    public static Node method2(Node head){
        if (head == null) return null;

        Node cur = head;

        while (cur != null){
           Node flag = new Node(cur.value);
           flag.next = cur.next;
           cur.next = flag;
           cur = flag.next;
        }

        cur = head;

        while (cur != null){
            cur.next.random = cur.random == null ? null : cur.random.next;
            cur = cur.next.next;
        }

        cur = head;
        Node next;
        Node n = head.next;
        while (cur != null){
            next = cur.next;
            cur.next = next.next;
            cur = next.next;
            next.next = cur == null ? null : cur.next;
        }
        return n;
    }

    public static void print(Node head){
        Node cur = head;

        System.out.println("\norder: ");
        while (cur != null){
            System.out.print(cur.value + " ");
            cur = cur.next;
        }

        System.out.println("\nrandom: ");
        cur = head;
        while (cur != null){
            System.out.print(cur.random == null ? "- " : cur.value + "->" + cur.random.value + " ");
            cur = cur.next;
        }

        System.out.println();
    }

    public static void main(String[] args) {
        Node head = null;
        Node res1 = null;
        Node res2 = null;
        print(head);
        res1 = method1(head);
        print(res1);
        res2 = method2(head);
        print(res2);
        print(head);
        System.out.println("=========================");

        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.random = head.next.next.next.next.next; // 1 -> 6
        head.next.random = head.next.next.next.next.next; // 2 -> 6
        head.next.next.random = head.next.next.next.next; // 3 -> 5
        head.next.next.next.random = head.next.next; // 4 -> 3
        head.next.next.next.next.random = null; // 5 -> null
        head.next.next.next.next.next.random = head.next.next.next; // 6 -> 4

        print(head);
        res1 = method1(head);
        print(res1);
        res2 = method2(head);
        print(res2);
        print(head);
        System.out.println("=========================");

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值