138. Copy List with Random Pointer

原创 2017年08月13日 07:55:17

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.

code

注意某个节点没有next,但是有random的情况

/**
 * Definition for singly-linked list with a random pointer.
 * public class RandomListNode {
 *     public int label;
 *     public RandomListNode next, random;
 *     public RandomListNode(int x) { this.label = x; }
 * };
 */

public class Solution {
    public RandomListNode CopyRandomList(RandomListNode head) {
        if(head==null) return head;
        RandomListNode clonehead = new RandomListNode(head.label);
        if(head.next!=null)
            clonehead.next = new RandomListNode(head.next.label);
        if(head.random!=null)
            clonehead.random = new RandomListNode(head.random.label);       
        RandomListNode next = CopyRandomList(head.next);
        var cur = clonehead.next;
        if(next!=null){
            cur.next = next.next;
            cur.random = next.random;
        }    
        return clonehead;
    }
}
版权声明:本文为博主原创文章,欢迎转载,请注明 http://blog.csdn.net/daigualu

【LeetCode-面试算法经典-Java实现】【138-Copy List with Random Pointer(拷贝有随机指针的单链表)】

【138-Copy List with Random Pointer(拷贝有随机指针的单链表)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  A linked list ...

138. Copy List with Random Pointer

题目 复制带有随机指针的链表:一个单链表除了next指针外还有一个random指针随机指向任何一个元素(可能为空)  《剑指offer》上的面试题26 分析 方法一:  map,先按...

leetcode题解-138. Copy List with Random Pointer

题目:A linked list is given such that each node contains an additional random pointer which could poin...

【leetcode每日一题】138.Copy List with Random Pointer

题目: A linked list is given such that each node contains an additional random pointer which could ...

LeetCode 138. Copy List with Random Pointer

和LeetCode 133. Clone Graph一个意思, 深层拷贝意味着,对a进行拷贝,获得b, 使得b的成员变量不仅值与a中对应的成员变量相等,同时它们指向不同的内存空间。 维护一张map, ...

[Leetcode] 138. Copy List with Random Pointer 解题报告

题目: A linked list is given such that each node contains an additional random pointer which coul...

[leetcode] 138. Copy List with Random Pointer 解题报告

题目链接:https://leetcode.com/problems/copy-list-with-random-pointer/ A linked list is given such that...

Leetcode 133. Clone Graph & 138. Copy List with Random Pointer

因为自己是先做的138,先说138。 138. Copy List with Random Pointer Total Accepted: 64859 Total Submissions:...

Leetcode-138. Copy List with Random Pointer

题目A linked list is given such that each node contains an additional random pointer which could point...

LeetCode138 Copy List with Random Pointer(深度复制带有随机指针的链表) Java题解

题目: A linked list is given such that each node contains an additional random pointer which coul...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:138. Copy List with Random Pointer
举报原因:
原因补充:

(最多只允许输入30个字)