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

[Leetcode] Copy List with Random Pointer

阅读更多

Copy List with Random Pointer

 
AC Rate: 350/1573
My Submissions

 

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.

 

 

/**
 * Definition for singly-linked list with a random pointer.
 * struct RandomListNode {
 *     int label;
 *     RandomListNode *next, *random;
 *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 * };
 */
class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        if (head == NULL) return NULL;
        RandomListNode* cur = head;
        while (cur != NULL) {
            RandomListNode* x = new RandomListNode(cur->label);
            x->next = cur->next;
            cur->next = x;
            cur = x->next;
        }
        
        cur = head;
        while (cur != NULL) {
            if (cur->random != NULL) cur->next->random = cur->random->next;
            cur = cur->next->next;
        }
        RandomListNode myhead(99);
        cur = head;
        RandomListNode* newcur = &myhead;
        while (cur != NULL) {
            newcur->next = cur->next;
            newcur = newcur->next;
            cur->next = cur->next->next;
            cur = cur->next;
        }
        newcur->next = NULL;
        return myhead.next;
    }
};

 

分享到:
评论

相关推荐

    leetcode中文版-LeetCode:力码

    leetcode中文版 LeetCode/Cpp ...138.Copy List with Random Pointer LeetCode 142.Linked List Cycle II(solve1) LeetCode 142.Linked List Cycle II(solve2) LeetCode 160.Intersection of Two Linke

    dna匹配leetcode-leetcode:leetcode刷题

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

    两两认识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 前言 每天刷点leetcode,基于java语言实现。...Copy List with Random Pointer Building H2O Fizz Buzz Multithreaded hard Merge k Sorted Lists Reverse Nodes in k-Group Trapping Rain Water

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

    Copy List with Random Pointer, Populating Next Right Pointers in Each Node I && II) PIE (未录入) CC150 (未录入) EPI (未录入) 每一个题库对应problems路径下的一个文件夹,每一个题目对应相应题库下的一个...

    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 递归...

    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