【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 ...
随机推荐
- PHP 导入数据库 sql 文件
使用PHP 可以导入sql来建立数据库.代码如下: <?php $hostname = 'localhost'; $dbname = 'test'; $username = 'root'; $p ...
- IPC之AIDL&binder关系
binder是一个远程对象的基础类,核心部分是远程调用机制,这部分是由IBinder定义的.它是对IBinder类的实现,其中IBinder类提供这样一个类的标准的本地化实现方式. 大多数开发者不会去 ...
- Linux C笔记
<Linux C编程一站式学习>笔记 gcc编译 直接编译 gcc main.c a.out 指定可执行文件名 gcc main.c -o main main 提示所有的警告信息 gcc ...
- PHP之mb_strrpos使用
mb_strrpos (PHP 4 >= 4.0.6, PHP 5, PHP 7) mb_strrpos - Find position of last occurrence of a stri ...
- AngularJS 的常用特性(三)
6.表达式 在模板中使用表达式是为了以充分的灵活性在模板.业务逻辑和数据之间建立联系,同时又能避免让业务逻辑渗透到模板中. <div ng-controller="SomeContr ...
- assert函数的用法
assert这个函数在php语言中是用来判断一个表达式是否成立.返回true or false; 例如: <?php $s = 123; assert("is_int($s)" ...
- Leetcode 337. House Robber III
337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...
- [作业] Python入门基础---购物车小程序
1.购物车小程序: 1.1用户输入工资取60% 1.2打印输出商品菜单 1.3由用户输入数字选择 #__author:Mifen #date: 2018/11/27 # 购物车程序 #把工资作为账户的 ...
- Examples of GoF Design Patterns--摘录
http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns You can find an overview ...
- CentOS下安装Redis(转载)
Redis是一个高性能的,开源key-value型数据库.是构建高性能,可扩展的Web应用的完美解决方案,可以内存存储亦可持久化存储.因为要使用跨进程,跨服务级别的数据缓存,在对比多个方案后,决定使用 ...