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. KVC与KVO的进阶使用

    本篇主要介绍键-值编码KVC,键值观察KVO的进阶使用的一些技巧主要是一下两个方面: KVC的集合操作符 KVO的手动实现方式 KVC集合操作符 关于集合操作符在苹果官方文档搜索Collection ...

  2. 匹配session

    package dl.zhang.test.util; import java.io.IOException; import java.io.Reader; import org.apache.iba ...

  3. hdu----(2222)Keywords Search(ac自动机)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. hdu---(1325)Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  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. 图形界面报错“已拒绝X11转移申请”的解决方法

    今天想通过本机给虚拟机起x-manager图形界面的时候报出 解决办法: 1.原来X11 forwarding依赖“xorg-x11-xauth”软件包,所以必须先安装“xorg-x11-xauth” ...

  7. mybatis中的mapper.xml

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  8. 基于TCP协议的网络通信

    TCP/IP通信协议是一种可靠的网络协议,它在通信的两端各建立一个Socket,从而在通信的两端之间形成网络虚拟链路,一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路进行通信.Java对基于TC ...

  9. 使用AlarmManager定时更换壁纸----之一

    import android.os.Bundle;import android.app.Activity;import android.app.AlarmManager;import android. ...

  10. 一个app中保持程序全屏的方法。

    public void toggleFullscreen(boolean fullScreen) { //fullScreen为true时全屏 WindowManager.LayoutParams a ...