复杂链表的复制(辅助存储避免原址引用)

刚看到这个题目的时候想到的是利用三个队列进行存储三个域(一个队列也行),我当时第一次写出的代码如下:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;



 class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;

    RandomListNode(int label) {
        this.label = label;
    }
}

public class Solution {
	public static void main(String[] args) {
		RandomListNode r1=new RandomListNode(1);
		
		RandomListNode r2=new RandomListNode(2);
		RandomListNode r3=new RandomListNode(3);
		RandomListNode r4=new RandomListNode(4);
		RandomListNode r5=new RandomListNode(5);
		RandomListNode r6=new RandomListNode(6);
		r1.next=r2;
		r1.random=r6;
		r2.next=r3;
		r3.next=r4;
		r3.random=r5;
		r4.next=r5;
		r5.next=r6;
		r5.random=r2;
		r6.next=new RandomListNode(0);
		RandomListNode head=r1;
		while(head!=null) {
			System.out.println(head.label);
			head=head.next;
		}
		System.out.println("----------------------------");
		//r1.next=r2;
		RandomListNode n=Clone(r1);
		RandomListNode n1=n;
		while(n1!=null) {
			System.out.println(n1.label);
			n1=n1.next;
		}
	}
	public static RandomListNode Clone(RandomListNode pHead){
        if(pHead==null)
            return null;
        LinkedList<RandomListNode> nextll=new LinkedList<RandomListNode>();
        LinkedList<RandomListNode> randomll=new LinkedList<RandomListNode>();
        LinkedList<Integer> intll=new LinkedList<Integer>();
        RandomListNode head=pHead;
        while(head!=null){
            nextll.add(head.next);
            randomll.add(head.random);
            intll.add(head.label);
            head=head.next;
        }
        head=new RandomListNode(pHead.label);
        RandomListNode ans=head;
        while(!intll.isEmpty()){
            RandomListNode temp=new RandomListNode(intll.poll());
            temp.next=nextll.poll();
            temp.random=randomll.poll();
            ans.next=temp;
            ans=ans.next;
        }
        return head.next;
    }
		
	
}

乍一看可能感觉没啥问题,仔细想一下就会发现,队列中存储的仍然是原来链表的引用,所以用辅助存储的核心要点是生成新的对象。然后分别为此对象赋予next域和random域。

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class Solution {
    public RandomListNode Clone(RandomListNode pHead)
    {
        HashMap<RandomListNode,RandomListNode> map = new HashMap<RandomListNode,RandomListNode>();
        RandomListNode p = pHead;
        RandomListNode q = new RandomListNode(-1);
        RandomListNode Q = q;
        while(p!=null){
            RandomListNode t = new RandomListNode(p.label);
            map.put(p, t);
            p = p.next;
            q.next = t;
            q = t;
        }
        Set<Entry<RandomListNode,RandomListNode>> set = map.entrySet();        
        Iterator<Entry<RandomListNode,RandomListNode>> it = set.iterator();        
        while(it.hasNext()){
            Entry<RandomListNode, RandomListNode> next = it.next();            
            next.getValue().random = map.get(next.getKey().random);
        }
        return Q.next;
    }
}

关键注意这个语句:

next.getValue().random = map.get(next.getKey().random);

如果错误的写成这样:

next.getValue().random = next.getKey().random;

则提交就会不通过。原因就是第二条语句属于原址赋值,你新创建的对象的random域仍然属于原链表的引用,而第一条语句相当于利用了HashMap的Value域,Key域存储的是原链表的引用,Value域都是新生成的对象,所以保存的是新的对象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值