Problem link:

http://oj.leetcode.com/problems/copy-list-with-random-pointer/

Deepcopy a linked list with random pointer, realy simple problem, solved in three steps using a hash map:

  1. Create a new head, duplicating the head (return NULL for case head==NULL), map key=p1 with value=p2.
  2. Let p1 point to head and p2 point to the new head. For each p1.next != NULL, duplicate p1.next and assign the new created node to p2.next, then move p1 and p2 to the next node and map them.
  3. Scan each node of the old list again. For each node p, if p.random != None, set hash_map[p].random = hash_map[p.random].

The following code is my python solution accepted by oj.leetcode.

# Definition for singly-linked list with a random pointer.
# class RandomListNode:
# def __init__(self, x):
# self.label = x
# self.next = None
# self.random = None class Solution:
# @param head, a RandomListNode
# @return a RandomListNode
def copyRandomList(self, head):
if head is None:
return None
mapping = {}
# Duplicate the head
new_head = RandomListNode(head.label)
# Duplicate the node and next pointer
p1 = head
p2 = new_head
mapping[p1] = p2
while p1.next is not None:
p2.next = RandomListNode(p1.next.label)
p2 = p2.next
p1 = p1.next
mapping[p1] = p2
# Duplicate the random pointer
p1 = head
while p1 is not None:
r1 = p1.random
if r1 is not None:
mapping[p1].random = mapping[r1]
p1 = p1.next
# Return
return new_head

  

【LEETCODE OJ】Copy List with Random Pointer的更多相关文章

  1. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  2. LeetCode OJ:Copy List with Random Pointer(复制存在随机链接的链表)

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

  3. 【LEETCODE OJ】Clone Graph

    Problem link: http://oj.leetcode.com/problems/clone-graph/ This problem is very similar to "Cop ...

  4. 【leetcode】Copy List with Random Pointer (hard)

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

  5. 【Leetcode】【Hard】Copy List with Random Pointer

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

  6. 【LeetCode】Copy List with Random Pointer

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

  7. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  8. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  9. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

随机推荐

  1. Qt之属性系统

    简述 Qt提供一个类似于其它编译器供应商提供的复杂属性系统(Property System).然而,作为一个编译器和平台无关的库,Qt不能够依赖于那些非标准的编译器特性,比如:__property或者 ...

  2. file类之目录

    可以解决的问题是:                有时需要列出目录下指定类型的文件,比如java,txt等扩展名的文件,可以使用File类的下述两个方法,列出指定类型的文件. /* file类实现两个 ...

  3. LINUX 硬盘命令

    1. 查看硬盘情况fdisk -l 每个Disk 为一个硬盘2. 挂在新硬盘fdisk /dev/sdb #硬盘地址Command (m for help):n #新建立分区Command actio ...

  4. 数据库索引<一> 索引结构表结构

    有很长时间没有更新博客了,再过几天都2月分了,如果再不更新一篇,我1月分都没有更新,保持连续,今天更新一篇. 最近没有什么看技术方面的东西,游戏,画画搞这些去了.我发现我每年一到年底就是搞这些东西,其 ...

  5. 关于PATH_INFO SCRIPT_NAME SCRIPT_FILENAME REDIRECT_URL 详解

    参考:http://www.nginx.cn/426.html  http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/09/13/217507 ...

  6. xll

    http://www.aiuxian.com/article/p-2027873.html

  7. 使用Apache HttpClient

    在一般情况下,如果只是需要Web站点的某个简单页面提交请求并获取服务器响应,完全可以使用前面所介绍的HttpConnection来完成.但在绝大部分情况下,Web站点的网页可能没这么简单,这些页面并不 ...

  8. 使用MediaRecorder录制视频短片

    MediaRecorder除了可用于录制音频之外,还可用于录制视频,使用MediaRecorder录制视频与录制音频的步骤基本相同.只是录制视频时不仅需要采集声音,还需要采集图像.为了让MediaRe ...

  9. 【同行说技术】Python程序员小白变大神必读资料汇总( 三)

    在文章<Python开发.调试.爬虫类工具大全>里面向大家总结了各种实用工具和爬虫技术,今天小编收集了5篇带有实例干货的资料,赶紧来看看吧!另外,喜欢写博客的博主可以申请加工程师博主交流群 ...

  10. 全国行政区划代码(json对象版)

    var area = {"110000":"北京市","110100":"北京市","110101" ...