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. Pictures of Ascii Art

    简述 指尖上的艺术 - 通过键盘上韵律般的敲敲打打,一幅幅美轮美奂的艺术作品便跃然于屏. 这样的画作,包含了无穷的创意,糅合了现代计算机科技与传统绘画艺术,难道还有比这更令人陶醉的美妙事物吗? 简述 ...

  2. (19)odoo中的javascript

    -----------更新日期15:17 2016-02-16 星期二-----------* 用到的js库   我们可以打开 addons/web/views/webclient_template. ...

  3. Xcode8 及iOS10适配问题汇总

    上点干货,目前得知的一些bug解决汇总:iOS10相册相机闪退bughttp://www.jianshu.com/p/5085430b029fiOS 10 因苹果健康导致闪退 crashhttp:// ...

  4. ADO.NET事务处理,初始回调函数,多张表的数据在同一个DataGridView中展示

    执行ADO.NET事务包含四个步骤,接下来以Transaction对象为例介绍. (1)调用SQLConnection对象的BeginTransaction()方法,创建一个SQLTransactio ...

  5. git在公司内部的使用实践(转)

    从2011.10月左右,开始在后台组推行git版本控制,到现在也差不多半年了,也形成了一套基于git flow的副官模式工作流程: 版本定义: 版本号使用x.x.x进行定义,第一个x代表大版本只有在项 ...

  6. Zookeeper注册节点的掉线自动重新注册及测试方法

    转载:http://www.codelast.com/ 在一套分布式的online services系统中,各service通常不会放在一台服务器上,而是通过Zookeeper这样的东西,将自己的se ...

  7. JDE函数--获取当前登录人的描述

    业务描述:当前登录人ID为数字,中文姓名保存在描述1字段中 方式: 根据系统变量获取用户的地址号,根据TableIO获取用户描述1.如下图所示: 由于使用AN8时,变量类型不一致,所以使用函数将cha ...

  8. mysql有回滚,php没有回滚的说法

    mysql 事务表是有回滚的说法.当发生mysql层面的错误才会执行回滚

  9. js中的运算符优先级顺序

    js中运算符优先级从高到底的顺序: 算术操作符 → 比较操作符 → 逻辑操作符 → "="赋值符号

  10. ViewPager滑动页面的实现方法

    package com.lixu.pagerview; import java.util.ArrayList; import android.app.Activity; import android. ...