关闭

138. Copy List with Random Pointer

标签: listleetcodeclone
3962人阅读 评论(0) 收藏 举报
分类:

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;
    }
}
2
0
查看评论

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

【138-Copy List with Random Pointer(拷贝有随机指针的单链表)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  A linked list is given such that each node contains an additiona...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-18 06:26
  • 2177

LeetCode138 Copy List with Random Pointer(深度复制带有随机指针的链表) 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...
  • u012249528
  • u012249528
  • 2015-07-28 21:32
  • 955

leetcode_c++:哈希: 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 ...
  • mijian1207mijian
  • mijian1207mijian
  • 2016-07-06 14:49
  • 112

[Leetcode 138] Copy List with Random Pointer

题目 复制带有随机指针的链表:一个单链表除了next指针外还有一个random指针随机指向任何一个元素(可能为空) 《剑指offer》上的面试题26 分析 代码
  • yiting52
  • yiting52
  • 2015-07-27 10:42
  • 980

Copy List with Random Pointer -- LeetCode

原题链接: http://oj.leetcode.com/problems/copy-list-with-random-pointer/  这是一道链表操作的题目,要求复制一个链表,不过链表的每个结点带有一个随机指针,指向某一个结点。 我们先介绍一种比较直接的算法,思路是先按...
  • linhuanmars
  • linhuanmars
  • 2014-03-29 05:10
  • 12418

138. 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 co...
  • hyperbolechi
  • hyperbolechi
  • 2015-01-24 11:24
  • 504

[Leetcode][python]Copy List with Random Pointer

题目大意一个链表中的每一个节点都有一个额外的随机指针,指向链表中的任意节点或空节点。对这个链表进行深拷贝。(要拷贝随即指针)解题思路有两种思路,参考: http://bookshadow.com/weblog/2015/07/31/leetcode-copy-list-random-pointer...
  • qqxx6661
  • qqxx6661
  • 2017-11-21 02:47
  • 49

**Leetcode_copy-list-with-random-pointer (c++ and python version)

地址:http://oj.leetcode.com/problems/copy-list-with-random-pointer/ A linked list is given such that each node contains an additional random pointer w...
  • flyupliu
  • flyupliu
  • 2014-03-22 22:18
  • 533

[LeetCode] 138. Copy List with Random Pointer

LeetCode 链表
  • hiimdaosui
  • hiimdaosui
  • 2016-09-07 10:42
  • 80

Leetcode 138. Copy List with Random Pointer

#include <iostream> using namespace std; struct RandomListNode { int label; RandomListNode *next, *random; RandomListNode(int x) : l...
  • taoyanqi8932
  • taoyanqi8932
  • 2016-07-02 21:24
  • 144
    算法channel

    交流思想,注重分析,实例阐述,通俗易懂,包含但不限于:经典算法,机器学习,深度学习,LeetCode 题解,Kaggle 实战。期待您的到来!

    算法与人工智能交流群:646901659

    个人资料
    • 访问:320961次
    • 积分:7603
    • 等级:
    • 排名:第3381名
    • 原创:350篇
    • 转载:1篇
    • 译文:0篇
    • 评论:58条
    博客专栏