剑指offer题24

48 篇文章 0 订阅
22 篇文章 2 订阅
package jianzhioffer;

/**复杂链表的复制:
 * 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),
 * 返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
 *
 */

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

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

public class Solution24 {
	
	 /*public static RandomListNode Clone(RandomListNode pHead){
		 RandomListNode clone = pHead;
		 return clone;
	 }*/
	
//	递归方法:转化为一个头结点和除去头结点的剩余部分,剩余部分操作和原问题一致
	 public static RandomListNode Clone(RandomListNode pHead){
		 if(pHead == null){
			 return null;
		 }
		 //开辟新节点,作为头节点
		 RandomListNode clone = new RandomListNode(pHead.label);
		 clone.next = pHead.next;
		 clone.random = pHead.random;
		 //递归处理剩余部分节点
		 clone.next = Clone(pHead.next);		 
		 return clone;
	 }
	
//	三步法:1、遍历链表,复制每个结点,如复制结点A得到A1,将结点A1插到结点A后面;
//	     2、重新遍历链表,复制老结点的随机指针给新结点,如A1.random = A.random.next;
//	     3、拆分链表,将链表拆分为原链表和复制后的链表
	/*public static RandomListNode Clone(RandomListNode pHead) {
		if(pHead == null){
			return null;
		}
		
		RandomListNode currentNode = pHead;
		while(currentNode != null){
			RandomListNode clone = new RandomListNode(currentNode.label);
			clone.next = currentNode.next;
			currentNode.next = clone;
			currentNode = clone.next;
		}
		
		currentNode = pHead;
		while(currentNode != null){
			if(currentNode.random == null){
				currentNode.next.random = null;
			}else{
				currentNode.next.random = currentNode.random.next;
			}
//			currentNode.next.random = currentNode.random==null?null:currentNode.random.next;
			currentNode = currentNode.next.next;
		}
		
		currentNode = pHead;
		RandomListNode  pclone = pHead.next;
		while(currentNode != null){
			RandomListNode clone = currentNode.next;
			currentNode.next = clone.next;
			clone.next = clone.next == null?null:clone.next.next;
			currentNode = currentNode.next;
		}
		return pclone;
	}*/
	
	
	
	 public static void main(String[] args) {
		RandomListNode pHead = new RandomListNode(2);
		RandomListNode node1 = new RandomListNode(0);
		RandomListNode node2 = new RandomListNode(1);
		RandomListNode node3 = new RandomListNode(7);
		RandomListNode node4 = new RandomListNode(4);
		RandomListNode node5 = new RandomListNode(5);
		
		pHead.next = node1;
		pHead.random = node4;
		node1.next = node2;
		node1.random = node3;
		node2.next = node3;
		node2.random = node5;
		node3.next = node4;
		node3.random = node2;
		node4.next = node5;
		node4.random = node1;
		node5.next = null;
		node5.random =null;
		
		/*//根据next输出原始链表:201745
		while(pHead!= null){
			System.out.println(pHead.label);
			pHead = pHead.next;
		}
		*/
		
		/*//根据random输出原始链表:240715
		while(pHead!= null){
			System.out.println(pHead.label);
			pHead = pHead.random;
		}
		*/
		
	    RandomListNode clone = Clone(pHead);
	    
	    //根据next输出复制的链表
		while(clone!= null){
			System.out.println(clone.label);
			clone = clone.next;
		}
		
		//根据random输出复制的链表
		while(clone!= null){
			System.out.println(clone.label);
			clone = clone.random;
		}   
	 }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值