【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 random pointer.
 * class RandomListNode {
 *     int label;
 *     RandomListNode next, random;
 *     RandomListNode(int x) { this.label = x; }
 * };
 */

【题意】

深拷贝一个链表,链表除了含有next指针外,还包含一个random指针,该指针指向字符串中的某个节点或者为空。

【思路一】(来自网络)

假设原始链表如下,细线表示next指针,粗线表示random指针,没有画出的指针均指向NULL:

构建新节点时,指针做如下变化,即把新节点插入到相应的旧节点后面:

【Java代码】

public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        if (head == null) return null;

        //第一遍扫描:对每个结点进行复制,把复制出来的新结点插在原结点之后
        RandomListNode node = head;
        while (node != null) {
            RandomListNode newnode = new RandomListNode(node.label);
            newnode.next = node.next;
            node.next = newnode;
            node = newnode.next;
        }

        //第二遍扫描:根据原结点的random,给新结点的random赋值
        node = head;
        while (node != null) {
            if (node.random != null) node.next.random = node.random.next;
            node = node.next.next;
        }

        RandomListNode newhead = head.next;

        //第三遍扫描:把新结点从原链表中拆分出来
        node = head;
        while (node != null) {
            RandomListNode newnode = node.next;
            node.next = newnode.next;
            if (newnode.next != null) newnode.next = newnode.next.next;
            node = node.next;
        }

        return newhead;
    }
}

【参考】

http://www.cnblogs.com/TenosDoIt/p/3387000.html

http://blog.csdn.net/linhuanmars/article/details/22463599

时间: 2024-10-03 13:27:23

【LeetCode】Copy List with Random Pointer 解题报告的相关文章

[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: 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

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://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题目:给定一个链表,当中的每一个节

[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. 利用hashMap存储,原始的node作为key,new的node作为value,这样就建立了新旧链表的以一一对应关系: /** * Definition for sing

[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. 这道链表的深度拷贝题的难点就在于如何处理随机指针的问题,由于每一个节点都有一个随机指针,这个指针可以为空,也可以指向链表的任意一个节点,如果我们在每生成一个新节点给其随机指

LeetCode——Copy List with Random Pointer(带random引用的单链表深拷贝)

问题: 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. * class

[LeetCode]Copy List with Random Pointer复杂链表的复制

/** * 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 {//为了能够快速定位某个节点,采用确定性映射的方式,将

leetcode - Copy List with Random Pointer题解

链表格式 通常链表就是一个值,一个next指针,指向后面的节点. 结构体如下: struct Node{ int val; struct Node* next; } 这个题目里的节点多了一个指针,除了指向下一个节点的next指针,还有一个指向这个链表随机一个节点的random指针. 结构体如下: struct Node{ int val; struct Node* next; struct Node* random; } 普通链表值拷贝 通常我们拷贝链表的时候,进行值拷贝,只用从头开始遍历就可以

【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