算法:深拷贝链表,其中链表有个随机指向的指针Copy List with R
算法:深拷贝链表,其中链表有个随机指向的指针Copy List with Random Pointer
题目
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 copy of the list.
The Linked List is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
val: an integer representing Node.val
random_index: the index of the node (range from 0 to n-1) where random pointer points to, or null if it does not point to any node.
Example 1:
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
Example 2:
Input: head = [[1,1],[2,1]]
Output: [[1,1],[2,1]]
Example 3:
Input: head = [[3,null],[3,0],[3,null]]
Output: [[3,null],[3,0],[3,null]]
Example 4:
Input: head = []
Output: []
Explanation: Given linked list is empty (null pointer), so return null.
Constraints:
-10000 <= Node.val <= 10000
Node.random is null or pointing to a node in the linked list.
The number of nodes will not exceed 1000.
思路分析
next,random都需要指向对象,那么就需要一个字典,key为对象,value为深拷贝的新对象。两次循环即可:
第一次循环组装字典,
第二次循环设置next,random。
返回结果为dict[head]
解法一:手动复制链表到字典里面
def copyRandomList(self, head: 'Node') -> 'Node':
dict = {}
m = n = head
while m:
dict[m] = Node(m.val)
m = m.next
while n:
dict[n].next = dict.get(n.next)
dict[n].random = dict.get(n.random)
n = n.next
return dict.get(head)
解法二:用系统方法先深拷贝整个链表
import collections
def copyRandomListWithOneRound(self, head: 'Node') -> 'Node':
dic = collections.defaultdict(lambda: Node(0))
dic[None] = None
n = head
while n:
dic[n].val = n.val
dic[n].next = dic[n.next]
dic[n].random = dic[n.random]
n = n.next
return dic[head]
算法:深拷贝链表,其中链表有个随机指向的指针Copy List with R相关教程
【数据结构与算法】简单排序(冒泡排序、选择排序、插入排序)完
【数据结构与算法】简单排序(冒泡排序、选择排序、插入排序)完整思路,并用代码封装排序函数 【数据结构与算法】简单排序(冒泡排序、选择排序、插入排序)完整思路,并用代码封装排序函数 本系列文章【数据结构与算法】所有完整代码已上传 github ,想要
【数据结构】线性表之单向链表
【数据结构】线性表之单向链表 并发编程模型 燕归来兮 https://www.zhoutao123.com/page/book/11 数据结构与算法 燕归来兮 https://www.zhoutao123.com/page/book/13 链表通常指的是单向链表,他包含多个节点,每个节点都有一个纸箱下一个几点的next指针。表
LeetCode#23 合并K个升序链表
LeetCode#23 合并K个升序链表 给你一个链表数组,每个链表都已经按升序排列。 请你将所有链表合并到一个升序链表中,返回合并后的链表。 使用递归分治法 以下是“治”的步骤演示 class Solution { public ListNode mergeKLists(ListNode[] lists) { return me
算法题之最长有效括号的长度问题
算法题之最长有效括号的长度问题 问题描述 思路分析 代码实现 public int longestValidParentheses(String s) { if (s == null || s.length() = 1) { return 0; } //定义一个栈 StackInteger stack = new Stack(); int len = s.length(); //定义一个maxLen in
机器学习算法,聚类和相似度(相似文档推荐案例)
机器学习算法,聚类和相似度(相似文档推荐案例) 聚类和相似度 1、[找到一种自动索引检索他感兴趣文章的方法] 1、如何衡量文章之间的相似度(需要一种度量手段) 2、怎样在全部的文章中搜索出下一篇要推荐的文章 2、如何表示一篇文档 词袋模型(忽略文档中单
数据结构与算法笔记
数据结构与算法笔记 排序原理: 1.比较相邻的元素。如果前一个元素比后一个元素大,就交换这两个 元素的位置。 2.对每一对相邻元素做同样的工作,从开始第一对元素到结尾的最后一对元素。最终最后位置的元素就是最大值。 代码: package sort;import java.uti
基础算法题——解方程(高精度二分)
基础算法题——解方程(高精度二分) 解方程 题目链接 对于解方程,很容易联想到二分。 它的精度要控制在10 -7 以上,精度要求较高。 数学工具函数 x a :pow(x, a) lnx : log(x) #includebits/stdc++.husing namespace std; int main(){ int a, b, c; scanf(
Python刷leetcode--142.环形链表 II
Python刷leetcode--142.环形链表 II 感觉这种题没多大用,基本上就是锻炼逻辑思维,全靠背题(不是背代码,是背思路,没一点复用性) 如何推导出下面的x = (n - 1) (y + z) + z 相遇时slow指针走的距离为x+y 2 * (x+y) = x+y+n*(y+z) - x = (n - 1) (y + z) + z