leetcode copy-list-with-random-pointer(Java)

leetcode题目

  copy-list-with-random-pointer

题目描述

 * A linked list is given such that each node 
 * contains an additional random pointer 
 * which could point to any node in the list or null.
 * Return a deep copy of the list.

思路

 * 1、复制next指针,复制链表的每一个节点,放在每个源节点的后面,源节点的next指针指向复制出来的节点
 * 2、复制random指针,复制的节点,复制他的前一个节点的random指针
 * 3、拆分链表,取偶数序号的元素组成新的链表

代码

package com.leetcode.list;

/**
 * 题目:
 * copy-list-with-random-pointer
 * 
 * 题目描述:
 * 
 * A linked list is given such that each node 
 * contains an additional random pointer 
 * which could point to any node in the list or null.
 * Return a deep copy of the list.
 */
public class CopyComplexList {

	static class RandomListNode {
		int label;
		RandomListNode next, random;
	    RandomListNode(int x) { this.label = x; }
        @Override
        public String toString() {
            if (this.next == null) {
                return String.valueOf(this.label);
            }
            return this.label + "(" +(this.random==null?"":this.random.label) + ")"
            + "->" + this.next.toString();
        }
	};
	
	/**
	 * 思路:
	 * 1、复制next指针,复制链表的每一个节点,放在每个源节点的后面,源节点的next指针指向复制出来的节点
	 * 2、复制random指针,复制的节点,复制他的前一个节点的random指针
	 * 3、拆分链表,取偶数序号的元素组成新的链表
	 * 
	 * @param head 头节点
	 */
    public RandomListNode copyRandomList(RandomListNode head) {
    	if (head == null) {
    		return null;
    	}
    	// 1、复制节点,放在源节点后面
    	RandomListNode cur = head;
    	while (cur != null) {
    		// 缓存下一个节点
    		RandomListNode next = cur.next;
    		// copy next指针即节点的值
    		RandomListNode copy = new RandomListNode(cur.label);
    		cur.next = copy;
    		copy.next = next;
    		// cur指向下一个节点
    		cur = next;
    	}
    	// 2、复制random指针
    	cur = head;
    	while (cur != null && cur.next != null) {
    		RandomListNode node = cur.random;
    		if (node != null) {
    			cur.next.random = node.next;
    		}
    		cur = cur.next.next;
    	}
    	// 3、取偶数序号的元素组成链表
    	RandomListNode newHead = head.next;
    	cur = newHead;
    	while (cur != null && cur.next != null) {
    		cur.next = cur.next.next;
    		cur = cur.next;
    	}
    	return newHead;
    }
    
	public static void main(String[] args) {
		RandomListNode head = createTestLinkedList();
		RandomListNode newHead = new RandomListNode(13);
		newHead.next = head;
		head.random = head.next.next;
		System.out.println(newHead);
		System.out.println(new CopyComplexList().copyRandomList(newHead));
	}
	
    private static RandomListNode createTestLinkedList() {
    	RandomListNode head = new RandomListNode(0);
    	RandomListNode curNode = head;
        for (int i = 1; i < 10; i++) {
            curNode.next = new RandomListNode(i);
            curNode = curNode.next;
        }
        return head;
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值