zoukankan      html  css  js  c++  java
  • [Java]LeetCode138. 复制带随机指针的链表 | Copy List with Random Pointer

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/9967399.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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.


    给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。

    要求返回这个链表的深度拷贝。 


    1ms

     1 /**
     2  * Definition for singly-linked list with a random pointer.
     3  * class RandomListNode {
     4  *     int label;
     5  *     RandomListNode next, random;
     6  *     RandomListNode(int x) { this.label = x; }
     7  * };
     8  */
     9 public class Solution {
    10     public RandomListNode copyRandomList(RandomListNode head) {
    11         RandomListNode res = new RandomListNode(0);
    12         RandomListNode p = res;
    13         RandomListNode curr = head;
    14         while (curr != null) {
    15             p.next = clone(curr);
    16             curr = curr.next;
    17             p = p.next;
    18         }
    19         return res.next;
    20     }
    21     
    22     private RandomListNode clone(RandomListNode node) {
    23         RandomListNode newNode = new RandomListNode(node.label);
    24         if (node.random != null)
    25             newNode.random = new RandomListNode(node.random.label);
    26         return newNode;
    27     }
    28 }

    2ms

     1 /**
     2  * Definition for singly-linked list with a random pointer.
     3  * class RandomListNode {
     4  *     int label;
     5  *     RandomListNode next, random;
     6  *     RandomListNode(int x) { this.label = x; }
     7  * };
     8  */
     9 public class Solution {
    10     public RandomListNode copyRandomList(RandomListNode head) {
    11         if(head == null) return head;
    12         copyNext(head);
    13         copyRandom(head);
    14         
    15         RandomListNode curr = head;
    16         RandomListNode newHead = head.next;
    17         RandomListNode start = newHead;
    18         while(curr!=null && curr.next!=null){
    19             RandomListNode next = curr.next.next;
    20             curr.next = next;
    21             curr = next;
    22             
    23             if(start.next!=null) {
    24                 start.next = start.next.next;
    25                 start = start.next;
    26             }
    27         }
    28         
    29         return newHead;
    30     }
    31     
    32     private void copyNext(RandomListNode head){
    33         if(head == null) return;
    34         
    35         RandomListNode curr = head;
    36         while(curr!=null){
    37             RandomListNode next = curr.next;
    38             curr.next = new RandomListNode(curr.label);
    39             curr.next.random = curr.random;
    40             curr.next.next = next;
    41             curr = next;
    42         }
    43     }
    44     
    45     private void copyRandom(RandomListNode head) {
    46         if(head == null) return;
    47         RandomListNode curr = head;
    48         while(curr!=null && curr.next!=null){
    49             RandomListNode next = curr.next.next;
    50             if(curr.next!=null && curr.random!=null){
    51                 curr.next.random = curr.random.next;
    52             }
    53             curr = next;
    54         }
    55     }
    56 }

    2ms

     1 /**
     2  * Definition for singly-linked list with a random pointer.
     3  * class RandomListNode {
     4  *     int label;
     5  *     RandomListNode next, random;
     6  *     RandomListNode(int x) { this.label = x; }
     7  * };
     8  */
     9 public class Solution {
    10     public RandomListNode copyRandomList(RandomListNode head) 
    11     {
    12         RandomListNode root = null, prev = null, rootIter = head;
    13         
    14         while (rootIter != null)
    15         {
    16             RandomListNode newCopy = new RandomListNode(rootIter.label);
    17             newCopy.next = null;
    18             newCopy.random = null;
    19             
    20             prev = rootIter;
    21             rootIter = rootIter.next;
    22             
    23             prev.next = newCopy;
    24             newCopy.next = rootIter;
    25             /* 'prev' points to previous rootIter whose 'next' will point
    26             to the new copy of previous rootIter created in 'newCopy' */
    27         }
    28         if (head == null)
    29             root = null;
    30         
    31         else
    32             root = head.next;
    33         
    34         rootIter = head;
    35         while (rootIter != null)
    36         {
    37             if (rootIter.random == null)
    38                 rootIter.next.random = null;
    39             
    40             else
    41                 rootIter.next.random = rootIter.random.next;
    42             
    43             rootIter = rootIter.next.next;
    44         }
    45         
    46         rootIter = head;
    47         while (rootIter != null)
    48         {
    49             RandomListNode nextNode = rootIter.next.next;
    50             if (nextNode != null)
    51                 rootIter.next.next = nextNode.next;
    52             
    53             rootIter.next = nextNode;
    54             rootIter = nextNode;  
    55         }
    56         return root;
    57     }
    58 }

    3ms

     1 /**
     2  * Definition for singly-linked list with a random pointer.
     3  * class RandomListNode {
     4  *     int label;
     5  *     RandomListNode next, random;
     6  *     RandomListNode(int x) { this.label = x; }
     7  * };
     8  */
     9 public class Solution {
    10     
    11     private Map<RandomListNode,RandomListNode> visited = new HashMap<>(); 
    12     
    13     public RandomListNode copyRandomList(RandomListNode head) {
    14         if(head == null){
    15             return null;
    16         }
    17         
    18         if(visited.containsKey(head)){
    19             return visited.get(head); 
    20         }
    21         
    22         RandomListNode newNode = new RandomListNode(head.label); 
    23         visited.put(head,newNode); //at this point in time, we have newed this node. does not matter if we need to recurse further to have it fully constructed. this is already copied. thus mark it as visited
    24         
    25         newNode.next = copyRandomList(head.next); 
    26         newNode.random = copyRandomList(head.random); 
    27         
    28         
    29         return newNode; 
    30         
    31     }
    32 }
  • 相关阅读:
    MySQL------代码1024,can't get hostname for your address解决方法
    MySQL------存储过程的使用
    MyEclipse------如何添加jspsmartupload.jar,用于文件上传
    JQuery------实现鼠标点击和滑动不同效果
    CSS------如何让div中的div处于右下角
    JQuery------制作div模态框
    CSS------Filter属性的使用方法
    python使用元类
    python __new__()分析
    centos自带python2.6升级到python2.7。并解决yum pip easy_install pip等模块兼容性问题
  • 原文地址:https://www.cnblogs.com/strengthen/p/9967399.html
Copyright © 2011-2022 走看看