【Leetcode】【Hard】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.
解题思路:
本题要求新建一个链表,使其完全等于输入链表,相当于对输入链表深复制。
题目的难点在于,原链表中有random指针,指向原链表对应的结点。当新链表建立时,需要对新结点的random指针赋值,使其指向新链表中的对应结点。这样就不能简单的复制地址,需要在新结点建立后,再找到对应正确的地址。暴力解法的时间复杂度为o(n2)。
o(n)的解法:
将每一个新结点,建立在旧结点的后面,形成:old1->new1->old2->new2->...oldN->newN->...;o(n)
建立后,重新遍历这个加长链表,new1->random = old1->random->next;o(n)
最后将新旧链表拆分;o(n)
最终时间复杂度为o(n),空间复杂度为o(1)
代码:
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (head == NULL)
return head; RandomListNode* oldNode = head;
RandomListNode* nodeLeft = NULL;
RandomListNode* newNode = NULL;
RandomListNode* newList = NULL; while (oldNode) {
nodeLeft = oldNode->next;
oldNode->next = new RandomListNode(oldNode->label);
oldNode->next->next = nodeLeft;
oldNode = nodeLeft;
} oldNode = head;
newList = head->next; while (oldNode) {
newNode = oldNode->next;
if (oldNode->random == NULL)
newNode->random = NULL;
else
newNode->random = oldNode->random->next;
oldNode = newNode->next;
} oldNode = head;
while (oldNode) {
newNode = oldNode->next;
oldNode->next = newNode->next;
oldNode = oldNode->next;
if (oldNode)
newNode->next = oldNode->next;
} return newList;
}
};
【Leetcode】【Hard】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题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- Copy List with Random Pointer leetcode java
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 16. Copy List with Random Pointer
类同:剑指 Offer 题目汇总索引第26题 Copy List with Random Pointer A linked list is given such that each node cont ...
- 133. Clone Graph 138. Copy List with Random Pointer 拷贝图和链表
133. Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of it ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- 【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 ...
随机推荐
- java面试题汇总(一)
1.MySQL之binlog底层原理分析:https://www.jianshu.com/p/e19d9312d1b5 2.redis持久化的几种方式https://www.cnblogs.com/A ...
- linux mint 19安装最新社区版docker
sudo apt-get update sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-p ...
- “融而开放、合以创新”T-HIM融合通信技术开发实战
本文来自腾讯云技术沙龙,本次沙龙主题为T-HIM融合通信技术开发实战 2018年,企业的数字化转型大规模兴起,"数字化经济"时代来临.如何利用数字化技术来支持业务的转型.增长与创新 ...
- POJ 3710:Matrix Power Series
Description 给出矩阵 \(n*n\) 的 矩阵\(A\) , 求 \(A^1+A^2+A^3...+A^k\) Solution 首先我们设 \(S_n=\sum_{i=1}^{n}A^i ...
- 仿58同城UITableViewCell动画
之前看58同城APP有一个页面中Cell依次从右向左移动,今天试着做了下. 在做的过程中也遇到了几个小的问题,也算是注意点吧. 1.Cell出现时每个Cell的动画时间一样,导致没有依次移动的效果. ...
- excel 工作表如何插入当前日期时间
在EXCEL表格中,插入当前的日期或是插入当前的时间:我们都可以用快捷键或时间函数来实现 插入当前日期 快捷键方法: 比如,显示日期的单元格为A1单元格: 今天是2018年6月8日: 鼠标点一下A1单 ...
- 如鹏网学习笔记(七)HTML基础
HTML笔记 一.HTML简介 1,HTML (Hyper Text Mark-up Language) 超文本标记语言,是一种编程语言,也可以说是一种标准.规范. 2,HTML提供了一系列标记(标签 ...
- LeetCode 第二天后续(两数相加 python3)
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # sel ...
- 理解B+树算法和Innodb索引
一.innodb存储引擎索引概述: innodb存储引擎支持两种常见的索引:B+树索引和哈希索引. innodb支持哈希索引是自适应的,innodb会根据表的使用情况自动生成哈希索引. B+树索引就是 ...
- 阿里云CentOS7.3配置Java Web应用和Tomcat步骤
阿里云的Linux系统包括CentOS7.3配置了密钥对 怎样将自己ECS实例绑定密钥对,并启用秘钥: https://help.aliyun.com/document_detail/51798.ht ...