zoukankan      html  css  js  c++  java
  • 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、负责后面一个节点,并且将这个节点插入到原来链表中  2、复制后面一个节点的random指针。 3 拆分组合链表为两部分。

    第一部分代码:

      while(currentnode!=null){//复制节点,并且将节点插入到该节点的后面
                  RandomListNode clonenode=new RandomListNode(currentnode.label);
                  clonenode.next=currentnode.next;
                  currentnode.next=clonenode;
                  currentnode=clonenode.next;
              }

    第二部分 代码:

      currentnode=head;
              while(currentnode!=null){//复制随机指针。
                  RandomListNode  clonenode=currentnode.next;
                  if(currentnode.random!=null){
                      clonenode.random=currentnode.random.next;//这里要指向随机指针的后一个,因为是复制滴啊,千万不要忘记了。
                  }
                  currentnode=clonenode.next;
              }

    第三部分代码:

      RandomListNode cloneHead=head.next;
              currentnode=head;
              while(currentnode.next!=null){
                  RandomListNode temp=currentnode.next;
                  currentnode.next=temp.next;
                  currentnode=temp;
              }

    总的代码:

    class RandomListNode {
         int label;
         RandomListNode next, random;
         RandomListNode(int x) { this.label = x; }
     };
    public class Solution {
          public RandomListNode copyRandomList(RandomListNode head) {
              if(head==null)return null;
              RandomListNode currentnode=head;
              while(currentnode!=null){//复制节点,并且将节点插入到该节点的后面
                  RandomListNode clonenode=new RandomListNode(currentnode.label);
                  clonenode.next=currentnode.next;
                  currentnode.next=clonenode;
                  currentnode=clonenode.next;
              }
              currentnode=head;
              while(currentnode!=null){//复制随机指针。
                  RandomListNode  clonenode=currentnode.next;
                  if(currentnode.random!=null){
                      clonenode.random=currentnode.random.next;//这里要指向随机指针的后一个,因为是复制滴啊,千万不要忘记了。
                  }
                  currentnode=clonenode.next;
              }
              
              RandomListNode cloneHead=head.next;
              currentnode=head;
              while(currentnode.next!=null){
                  RandomListNode temp=currentnode.next;
                  currentnode.next=temp.next;
                  currentnode=temp;
              }
              return cloneHead;
                
          }
    }
  • 相关阅读:
    [No000088]并行循环vs普通循环
    [No000087]Linq排序,SortedList排序,二分法排序性能比较
    [No000086]C#foreach集合被改变,报错处理方案
    [No000085]C#反射Demo,通过类名(String)创建类实例,通过方法名(String)调用方法
    [No000084]C# 使用Log4Net(1)-快速建立一个demo
    [No000082]Convert和Parse的区别/Convert.ToInt32()与int.Parse()的区别
    [No000081]SVN学习笔记1-服务端搭建
    [No00007F]2016-面经[下] 英文简历写作技巧
    [No00007E]2016-面经[中]
    [No00007D]2016-面经[上]
  • 原文地址:https://www.cnblogs.com/softwarewebdesign/p/5507543.html
Copyright © 2011-2022 走看看