`
blue2048
  • 浏览: 178703 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[leetcode]Copy List with Random Pointer - java

 
阅读更多

1. 第一次遍历深度克隆链表费random属性,并使用map记录一一对应的链表节点

2. 第二次遍历,根据map记录的节点关系, 补全random引用

 

/**
 * Definition for singly-linked list with a random pointer.
 * 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;
        }
        Map<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
        RandomListNode p = head;
        RandomListNode headNew = new RandomListNode(head.label);
        RandomListNode pNew = headNew;
        map.put(p, pNew);
        while (p != null){
            RandomListNode next = p.next;
            if(next == null){
                break;
            }
            RandomListNode nextNew = new RandomListNode(next.label);
            pNew.next = nextNew;
            p = p.next;
            map.put(next, nextNew);
            pNew = pNew.next;
        }
        p = head;
        pNew = headNew;
        while (p != null){
            RandomListNode rand = p.random;
            RandomListNode randNew = map.get(rand);
            pNew.random = randNew;
            p = p.next;
            pNew = pNew.next;
        }
        return headNew;
    }

}

 

 

分享到:
评论

相关推荐

    两两认识leetcode-copy-list-with-random-pointer:使用随机指针复制链表

    两两认识leetcode 使用随机指针复制链表 给出一个链表,使得每个节点都包含一个额外的随机指针,该指针可以指向链表中的任何节点或为空。 返回列表的深层副本。 链表在输入/输出中表示为 n 个节点的列表。 每个节点...

    LeetCode最全代码

    * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...

    多线程leetcode-leetcode-java:leetcode上的题解,基于java语言

    每天刷点leetcode,基于java语言实现。 leetcode上难度分三档:easy,medium,hard. 如下: easy medium Remove Nth Node From End of List Swap Nodes in Pairs Spiral Matrix Path Sum II Copy List with Random ...

    LeetCode:LeetCode解决方案

    preorder-traversal链表reorder-list链表linked-list-cycle-ii链表linked-list-cycle动态规划word-break-ii动态规划word-break链表copy-list-with-random-pointer复杂度single-number-ii复杂度single-number动态规划

    gasstationleetcode-leetcode-in-niuke:在牛客网上的在线编程中的leetcode在线编程题解

    copy-list-with-random-pointer 复杂度 single-number 动态规划 candy 贪心 gas-station 动态规划 palindrome-partitioning-ii 动态规划 triangle 树 sum-root-to-leaf-numbers 动态规划 distinct-subsequences 递归...

    dna匹配leetcode-leetcode:leetcode刷题

    dna匹配 leetcode leetcode刷题--C++ 哈希表 Longest Substring ...Pointer 单链表 map Max Points on a Line 斜率 map, int&gt; Fraction to Recurring Decimal map long long 正负号 Repeated DNA S

    leetcode中文版-LeetCode:力码

    leetcode中文版 LeetCode/Cpp 本人刷题记录在此,包含题意理解与算法思路,包含在Cpp文件内部注释,后续会持续更新。 有不懂的可以联系ji648513181,同时也欢迎志同道合O的朋友一起合作更新。 已更新剑指Offer答案...

    leetcode题库-pyshua:这是一个Python的编码判断系统

    leetcode题库 pyshua Python 算法题练习 用法: python Judge.py library problem 例子: python Judge.py leetcode TwoSum 如何贡献: 收录题库 LeetCode (还有4题未录入, 分别为 LRU Cache, Copy List with Random ...

    cpp-算法精粹

    Copy List with Random Pointer Linked List Cycle Linked List Cycle II Reorder List LRU Cache Palindrome Linked List 字符串 Valid Palindrome Implement strStr() String to Integer (atoi) Add Binary ...

    lrucacheleetcode-CPPS:CPPS

    List_with_Random_Pointer.py) + 16 散列 + 17 散列 [A](./hash_table/Insert_Delete_GetRandom_O(1).py) + 18 大批 + 19 二分查找 + 20 DP —— 21 堆 —— 22 散列 + 23 DP + 24 DP —— 25 DP + 26 堆 + 27 堆 ...

Global site tag (gtag.js) - Google Analytics