Copy List with Random Pointer leetcode

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.

Subscribe to see which companies asked this question

1、在链表中依次插入新的结点

2、构建新节点random指针:newNode->random = oldNode->random->next

3、恢复原始链表以及构建新链表:例如old->next = old->next->next,  new->next = new->next->next

RandomListNode *copyRandomList(RandomListNode *head) {
    if (head == nullptr)
        return nullptr;
    RandomListNode *iter = head;
    while (iter != nullptr)
    {
        RandomListNode *newNode = new RandomListNode(iter->label);
        newNode->next = iter->next;
        iter->next = newNode;
        iter = newNode->next;
    }
    iter = head;
    RandomListNode *iter1;
    while (iter != nullptr)
    {
        iter1 = iter->next;
        if(iter->random != nullptr)
            iter1->random = iter->random->next;
        iter = iter1->next;
    }
    RandomListNode *ret;
    ret = head->next;
    iter1 = ret;
    head->next = ret->next;
    iter = head->next;
    while (iter != nullptr)
    {
        iter1->next = iter->next;
        iter1 = iter1->next;
        iter->next = iter1->next;
        iter = iter->next;
    }
    return ret;
}
时间: 2024-10-20 21:34:04

Copy List with Random Pointer leetcode的相关文章

Copy List with Random Pointer leetcode 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. 题解: 如果要copy一个带有random pointer的list,主要的问题就是有可能这个random指向的位置还没有被copy到,所以解决方法都是多次扫描li

【LeetCode】Copy List with Random Pointer

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. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n

LeetCode: Copy List with Random Pointer [138]

[题目] 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. [题意] 给定一个链表,每个节点除了next指针外,还有一个random指针,指向任意的节点. 要求,复制这样的一个链表 [思路] 思路1: 先一次生成每个节点对

[leetcode]Copy List with Random Pointer @ Python

原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意: 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. 解题思路:这题主要是需要深

[LeetCode][JavaScript]Copy List with Random Pointer

Copy List with Random Pointer 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. https://leetcode.com/problems/copy-list-with-random-poin

leetcode -day8 Copy List with Random Pointer & Single Number I II

五一中间断了几天,开始继续... 1.  Copy List with Random Pointer 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. 分析:剑指offer上的一道题目,分三步进行,首先复制每个链表结点

leetcode题目:Copy List with Random Pointer

题目: 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 ran

[Leetcode][JAVA] Clone Graph, Copy List with Random Pointer

Clone Graph: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label a

leetcode--011 copy list with random pointer

1 package leetcode; 2 3 class RandomListNode { 4 int label; 5 RandomListNode next, random; 6 7 public RandomListNode(int x) { 8 this.label = x; 9 } 10 } 11 12 public class CopyListRandom { 13 public RandomListNode copyRandomList(RandomListNode head)