【LEETCODE OJ】Copy List with Random Pointer
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:
- Create a new head, duplicating the head (return NULL for case head==NULL), map key=p1 with value=p2.
- 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.
- 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的更多相关文章
- 【LeetCode练习题】Copy List with Random Pointer
Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...
- 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 ...
- 【LEETCODE OJ】Clone Graph
Problem link: http://oj.leetcode.com/problems/clone-graph/ This problem is very similar to "Cop ...
- 【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 ...
- 【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 ...
- 【LeetCode】Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
随机推荐
- 初学java之菜单条,菜单,菜单项的设置
package project; import javax.swing.*; import java.awt.event.KeyEvent; import java.awt.event.InputEv ...
- iOS开发中深入理解CADisplayLink和NSTimer
一.什么是CADisplayLink 简单地说,它就是一个定时器,每隔几毫秒刷新一次屏幕. CADisplayLink是一个能让我们以和屏幕刷新率相同的频率将内容画到屏幕上的定时器.我们在应用中创建一 ...
- java中的if-Switch选择结构
字随笔走,笔随心走,随笔,随心.纯属个人学习分析总结,如有观者还请不啬领教. 1.if选择结构 什么是if结构:if选择结构是根据判断结果再做处理的一种语法结构. 起语法是: if(判断条件){ 操作 ...
- SAP资产变动明细
*&---------------------------------------------------------------------* *& Report ZFIR027 * ...
- 超棒的响应式设计测试书签和工具(bookmarks)(转)
一.测试书签(bookmarks) Viewport Resizer 这个书签号称拥有158个国家3万多活跃的用户,主要特性: 完全自定制 方便的添加自定义尺寸 手动的横竖屏切换 自动的横竖屏切换 ( ...
- 转:Linux 安装 Mysql
前段时间安装了Mysql,但是有些问题,就想把他卸载了,重新安装一个,但是没想到在Linux卸载软件是一个很痛苦的事情. 我的Mysql是用命令的方式安装的,就是上一篇文章用到的那个命令(sudo ...
- Servlet+JSP+JavaBean开发模式(MVC)介绍
好伤心...写登陆注册之前看见一篇很好的博文,没有收藏,然后找不到了. 前几天在知乎上看见一个问题,什么时候感觉最无力. 前两天一直想回答:尝试过google到的所有solve case,结果bug依 ...
- ubuntu 设置静态ip
1. 为网卡配置静态IP地址 编辑文件/etc/network/interfaces: sudo vi /etc/network/interfaces 并用下面的行来替换有关eth0的行: # The ...
- POJ 1125 Stockbroker Grapevine 最短路 难度:0
http://poj.org/problem?id=1125 #include <iostream> #include <cstring> using namespace st ...
- 2.精通前端系列技术之JavaScript模块化开发 seajs(一)
在使用seajs模块化开发之前,直接在页面引用js会容易出现冲突及依赖相关的问题,具体问题如下 问题1:多人开发脚本的时候容易产生冲突(比如全局参数冲突,方法名冲突),可以使用命名空间降低冲突,不能完 ...