剑指offer题24
来源:互联网 发布:窗户漏风 知乎 编辑:程序博客网 时间:2024/06/05 02:12
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输出原始链表:201745while(pHead!= null){System.out.println(pHead.label);pHead = pHead.next;}*//*//根据random输出原始链表:240715while(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
- 剑指offer题24
- 剑指Offer-24
- 剑指offer第一题
- 剑指offer-题
- 剑指offer第一题
- 剑指Offer第一题
- 剑指offer(题三)
- 剑指offer第二题
- 剑指offer第三题
- 剑指offer第四题
- 剑指offer第五题
- 剑指offer题一
- 剑指offer题二
- 剑指offer题七
- 剑指offer题六
- 【剑指offer】第二题
- 【剑指offer】第三题
- 剑指offer题四
- POJ 2773, POJ 3906 有趣的容斥原理
- 云客Drupal8源码分析之用户角色实体
- 世界省市区数据库
- 苹果开发者账号注册申请流程(2017.6.27)
- dsp芯片是不是正在不断的边缘化?快被arm gpu碾压了?
- 剑指offer题24
- SOAPUI中文教程---测试步骤
- sklearn浅析(二)——Generalized Linear Models之一
- python 中__name__ = '__main__' 的作用
- NodeJS、NPM安装配置步骤(windows版本)
- 浅谈Java中的equals和==
- 第1.8章 scrapy之完整工程部署
- Polymer2.0安装与使用
- iOS逆向 开机启动APP