zoukankan      html  css  js  c++  java
  • 剑指offer-复杂链表的复制

    题目描述:

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

    思路:

    第一步:根据原始链表的每个节点N创建对应的N’,把N’链接到N的后面。

    第二步:设置复制出来的结点的random结点,假设原始链表上的N的random指向结点S,那么其对应复制出来的N’是N的next指向的结点,同样S’也是S的next指向的结点

    第三步:把这个长链表拆分成两个链表,偶数位置的结点链接起来就是复制出来的链表。

    public class Test {
    
        public static void main(String[] args) {
            Test t = new Test();
            ComplexListNode list1 = t.new ComplexListNode(1);
            ComplexListNode list2 = t.new ComplexListNode(2);
            ComplexListNode list3 = t.new ComplexListNode(3);
            ComplexListNode list4 = t.new ComplexListNode(4);
            ComplexListNode list5 = t.new ComplexListNode(5);
    
            list1.next = list2;
            list1.random = list3;
            list2.next = list3;
            list2.random = list5;
            list3.next = list4;
            list4.next = list5;
    
            ComplexListNode l = t.cloneList(list1);
            while (l != null) {
                System.out.print(l.value + " ");
                l = l.next;
            }
    
        }
    
        class ComplexListNode {
            int value;
            ComplexListNode next;
            ComplexListNode random;
    
            public ComplexListNode(int value) {
                this.value = value;
            }
        }
    
        public ComplexListNode cloneList(ComplexListNode head) {
            if (head == null)
                return null;
            // 第一步:根据原始结点N创建新结点N‘,N’链接到N后面
            // A-B-C-D-E
            // A-A'-B-B'-C-C'-D-D'-E-E'
            ComplexListNode node = head;
            while (node != null) {
                ComplexListNode cloneNode = new ComplexListNode(0);
                cloneNode.value = node.value;
                cloneNode.next = node.next;
                node.next = cloneNode;
                node = cloneNode.next;
            }
    
            // 第二步:设置复制出来的random.
            ComplexListNode node1 = head;
            while (node1 != null) {
                ComplexListNode cloneNode1 = node1.next;
                if (node1.random != null) {
                    cloneNode1.random = node1.random.next;
                }
                node1 = cloneNode1.next;
            }
    
            // 第三步:把长链表拆成两个链表,偶数位置 的为复制链表
            ComplexListNode nodeEven = null;
            ComplexListNode nodeEvenHead = null;
            ComplexListNode node2 = head;
            if (node2 != null) {
                nodeEven = nodeEvenHead = node2.next;
                node2.next = nodeEven.next;
                node2 = node2.next;
            }
            while (node2 != null) {
                nodeEven.next = node2.next;
                nodeEven = nodeEven.next;
                node2.next = nodeEven.next;
                node2 = node2.next;
            }
            return nodeEvenHead;
        }
    
    }
  • 相关阅读:
    洛谷P1443 马的遍历
    洛谷P1014 Cantor表
    《显示器件应用分析精粹:从芯片架构到驱动程序设计》已全面上市,活动赠书已经发放!
    透彻详尽的液晶显示屏CCFL背光源驱动逆变电源中的镇流电容设计
    《显示器件应用分析精粹:从芯片架构到驱动程序设计》正在印刷中,很快就要上市了
    透彻详细电荷泵升压电路的工作原理分析(配Multisim仿真验证)
    [GLSL]着色器周记03
    [GLSL]着色器周记02——火焰特效
    [GLSL]着色器周记01——真实光照
    毕业这五年【中】
  • 原文地址:https://www.cnblogs.com/zywu/p/5778792.html
Copyright © 2011-2022 走看看