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

一种特殊的链表节点类描述如下:(一个单链表的节点加了一条指针,加了一个rand指针。这个rand指针是随机指向某个节点的)

public class Node{
    public int value;
    public Node next;
    public Node rand;

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

要求:复制含有随机指针节点的链表(深度拷贝这个链表)

分析:假如1->2->3,1的rand指针指向3,2的rand指针指向1,3的rand指针指向null,1的拷贝节点是1’,2的拷贝节点是2’,3的拷贝节点是3’。

方法一:使用哈希表。在哈希表里面存一个对应关系(key,value),一个老节点对应它克隆出的新节点。再次遍历原始链表,当得到1的时候,map中可以把1’拿出来,1的next指针是指向2的,所以1’的next指针应该指向2的拷贝节点2’。然后通过2节点找到2’,把1’的next指针设成2’,然后通过1的rand指针找到3,那么1’的rand指针指向3的拷贝节点3’。以此类推2和3节点。

方法二:遍历老链表,但是拷贝出来的节点放在老链表的下一个,然后和老链表的再下一个相连。具体来说就是:遍历1的时候,生成1的拷贝节点1’,让1的next指针指向1’;1’的next指针指向2,遍历2的时候,生成2的拷贝节点2’,2’的next指针指向3;遍历3的时候,生成3的拷贝节点3’,3’的next指针指向null。到此结构变成1->1’->2->2’->3->null,而rand指针还没有发生变化。接下来遍历的时候,一次拿出两个节点,拿出1和1’,看看1’的rand指针如何设置。节点1通过1的rand指针指向3,3的克隆节点是3’即3的下一个节点,即通过3的next指针就可以找到3’。然后把1’的rand指针指向3’。节点2和3以此类推。最后一步,使新链表和老链表分离出来,整个过程结束。


import java.util.HashMap;

public class CopyListWithRandom {

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

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

	public static Node copyListWithRand1(Node head) {
		HashMap<Node, Node> map = new HashMap<Node, Node>();
		Node cur = head;
		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).rand = map.get(cur.rand);
			cur = cur.next;
		}
		return map.get(head);
	}

	public static Node copyListWithRand2(Node head) {
		if (head == null) {
			return null;
		}
		Node cur = head;
		Node next = null;
		// copy node and link to every node
		while (cur != null) {
			next = cur.next;
			cur.next = new Node(cur.value);
			cur.next.next = next;
			cur = next;
		}
		cur = head;
		Node curCopy = null;
		// set copy node rand
		while (cur != null) {
			next = cur.next.next;
			curCopy = cur.next;
			curCopy.rand = cur.rand != null ? cur.rand.next : null;
			cur = next;
		}
		Node res = head.next;
		cur = head;
		// split
		while (cur != null) {
			next = cur.next.next;
			curCopy = cur.next;
			cur.next = next;
			curCopy.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.value + " ");
			cur = cur.next;
		}
		System.out.println();
		cur = head;
		System.out.print("rand:  ");
		while (cur != null) {
			System.out.print(cur.rand == null ? "- " : cur.rand.value + " ");
			cur = cur.next;
		}
		System.out.println();
	}

	public static void main(String[] args) {
		Node head = null;
		Node res1 = null;
		Node res2 = null;
		printRandLinkedList(head);
		res1 = copyListWithRand1(head);
		printRandLinkedList(res1);
		res2 = copyListWithRand2(head);
		printRandLinkedList(res2);
		printRandLinkedList(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.rand = head.next.next.next.next.next; // 1 -> 6
		head.next.rand = head.next.next.next.next.next; // 2 -> 6
		head.next.next.rand = head.next.next.next.next; // 3 -> 5
		head.next.next.next.rand = head.next.next; // 4 -> 3
		head.next.next.next.next.rand = null; // 5 -> null
		head.next.next.next.next.next.rand = head.next.next.next; // 6 -> 4

		printRandLinkedList(head);
		res1 = copyListWithRand1(head);
		printRandLinkedList(res1);
		res2 = copyListWithRand2(head);
		printRandLinkedList(res2);
		printRandLinkedList(head);
		System.out.println("=========================");

	}

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值