剑指offer第二十五题复杂链表的复制

题目描述

输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针random指向一个随机节点),请对此链表进行深拷贝,并返回拷贝后的头结点。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

图(1)中,A.random = C; D.random = B; B.random = E.

图(2)中,A.random.next = C',也就是应该让A.next.random = C'.

下面的map.put(key, value)的方法简单点,这种复制也很好理解,哈哈~

package test;
class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;
    RandomListNode(int label) {
        this.label = label;
    }
}
class Solution {
    public RandomListNode Clone(RandomListNode pHead) {
        if(pHead == null) {
            return null;
        }

        RandomListNode currentNode = pHead;
        //1、复制每个结点,如复制结点A得到A1,将结点A1插到结点A后面;
        while(currentNode != null){
            RandomListNode cloneNode = new RandomListNode(currentNode.label);
            RandomListNode nextNode = currentNode.next;
            currentNode.next = cloneNode;
            cloneNode.next = nextNode;
            currentNode = nextNode;
        }

        currentNode = pHead;
        //2、重新遍历链表,复制老结点的随机指针给新结点,如A1.random = A.random.next;
        while(currentNode != null) {
            currentNode.next.random = currentNode.random==null?null:currentNode.random.next;
            System.out.println(currentNode.next.random);
            currentNode = currentNode.next.next;
        }

        //3、拆分链表,将链表拆分为原链表和复制后的链表
        currentNode = pHead;//A
        RandomListNode pCloneHead = pHead.next;//A'
        while(currentNode != null) {//currentNode = A
            RandomListNode cloneNode = currentNode.next;//cloneNode = A'
            currentNode.next = cloneNode.next;//A -> B
            cloneNode.next = cloneNode.next==null?null:cloneNode.next.next;
            currentNode = currentNode.next;
        }

        return pCloneHead;
    }
}
public class Main{
    public static void main(String[] args) {
        RandomListNode n1 = new RandomListNode(1);//804
        RandomListNode n2 = new RandomListNode(3);//805
        RandomListNode n3 = new RandomListNode(6);//806
        RandomListNode n4 = new RandomListNode(4);//807
        RandomListNode n5 = new RandomListNode(2);//808

        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        Solution solution = new Solution();
        RandomListNode list1=solution.Clone(n1);
        for(;list1 != null;){
            System.out.print("->" + list1.label);
            list1 = list1.next;
        }

    }
}

令一种方法:使用map(key, value)复制复杂链表。

package test;

import java.util.HashMap;

class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;
    RandomListNode(int label) {
        this.label = label;
    }
}
class Solution {
    public RandomListNode Clone(RandomListNode pHead)
    {
        HashMap<RandomListNode, RandomListNode> map = new HashMap<>();
        RandomListNode p = pHead;
        //第一次遍历 新建立节点
        while(p != null){
            RandomListNode newNode = new RandomListNode(p.label);
            map.put(p, newNode);
            p = p.next;
        }
        //第二次遍历 赋值映射关系
        p = pHead;
        while(p != null){
            RandomListNode node = map.get(p);//返回p对应的value
            node.next = (p.next == null)?null: map.get(p.next);//node这个地址有next和random属性
            node.random = (p.random == null)?null: map.get(p.random);
            p = p.next;
        }
        //最后的返回值
        return map.get(pHead);
    }
}
public class Main{
    public static void main(String[] args) {
        RandomListNode n1 = new RandomListNode(1);//804
        RandomListNode n2 = new RandomListNode(3);//805
        RandomListNode n3 = new RandomListNode(6);//806
        RandomListNode n4 = new RandomListNode(4);//807
        RandomListNode n5 = new RandomListNode(2);//808

        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        Solution solution = new Solution();
        RandomListNode list1=solution.Clone(n1);
        for(;list1 != null;){
            System.out.print("->" + list1.label);
            list1 = list1.next;
        }
    }
}

 

 

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页