剑指offer26 复杂链表的复制
/*
struct RandomListNode {
int label;
struct RandomListNode *next, *random;
RandomListNode(int x) :
label(x), next(NULL), random(NULL) {
}
};
*/
class Solution {
public:
RandomListNode* Duplicate(RandomListNode* pHead)
{
RandomListNode* p=pHead;
while(p)
{
RandomListNode* temp=(RandomListNode*)malloc(sizeof(RandomListNode));
temp->label=p->label;
temp->next=p->next;
p->next=temp;
p=temp->next;
}
return pHead;
} RandomListNode* RandomList(RandomListNode* pHead)
{
RandomListNode* p=pHead;
RandomListNode* pnext=NULL;
while(p)
{
pnext=p->next;
if(p->random)
{
pnext->random=p->random->next;
}
p=pnext->next;
}
return pHead;
}
RandomListNode* split(RandomListNode* pHead)
{
RandomListNode* result=pHead->next;
RandomListNode* p=pHead;
RandomListNode* p1=result;
while(p)
{
p->next=p1->next;
p=p->next;
if(p)
{
p1->next=p->next;
p1=p1->next;
}
}
return result;
} RandomListNode* Clone(RandomListNode* pHead)
{
if(!pHead)
return NULL;
RandomListNode* p1=Duplicate(pHead);
RandomListNode* p2=RandomList(p1);
RandomListNode* result=split(p2);
return result;
}
};
要注意各种指针为空的边界条件。在split函数中,必须一趟完成两个链表的拆分,不然就会出现超时错误。
剑指offer26 复杂链表的复制的更多相关文章
- 剑指Offer24 复杂链表的复制
/************************************************************************* > File Name: 24_Comple ...
- 剑指offer-复杂链表的复制
题目描述 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否 ...
- 剑指Offer——复杂链表的复制
题目描述: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用, ...
- 剑指offer-复杂链表的复制-链表-python
题目描述 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否 ...
- 剑指offer 复杂链表的复制 (有向图的复制)
时间复杂度O(3N) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ...
- 剑指Offer-25.复杂链表的复制(C++/Java)
题目: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否则 ...
- 用js刷剑指offer(复杂链表的复制)
题目描述 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否 ...
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- 《剑指offer》 链表中倒数第k个节点
本题来自<剑指offer> 链表中倒数第k个节点 题目: 输入一个链表,输出该链表中倒数第k个结点. 思路: 倒数第k个节点,而且只能访问一遍链表,定义两个节点,两者之间相差k个距离,遍历 ...
随机推荐
- ThinkPHP框架研究之一 基本函数 M和D的区别
http://my.oschina.net/wxweven/blog/56563?fromerr=32n4Nf7V https://segmentfault.com/q/101000000298807 ...
- Oracle EBS-SQL (PO-11):检查采购订单退货数.sql
select msi.segment1 物料编码, -- msi.inventory_item_id return_it ...
- 解决Delphi自带UTF8解码缺陷(使用API)
因为Delphi自带的转换函数遇到其无法识别的字符串就返回空,下面函数可解决该问题. function DecodeUtf8Str(const S: UTF8String): WideString;v ...
- Android stagefright与opencore对比
[转载至其它博客] http://blog.csdn.net/djy1992/article/details/9339917 1引言 Android froyo版本多媒体引擎做了变动,新添加了st ...
- JD-GUI on Ubuntu 13.04 64-bit
Java Decompiler (jd-gui) is a cute little tool I like using when working in Java. Unfortunately it o ...
- Android Activity 启动模式详解
最近有群里的朋友问我 Activity的四种启动模式分别是什么意思? 当初因为项目比较忙,草草的解释了下, Api文档中说的也只是一般,在这里就小记一下吧,以便有更多的朋友对Activity启动模式了 ...
- Conscription
Conscription Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- 关于VFP9.0备注字段(memo)插入编辑问题
最近在做项目 用VFP9.0这个比较古老的数据库,有个问题一直纠结我很久.就是memo这个备注字段,你在insert 的时候只要插入的字符串数据超过64K的时候就会出错. 之后我一直在找原因原来是备注 ...
- CRM Entity 之Money转string int类型等
Money转string 左右都是string //服务站地址 vehicleDetail["yt_servicestation_address"]=serviceStationC ...
- ajax相关
Javascript·部分: <script language="javascript" type="text/javascript" src=" ...