剑指Offer24 复杂链表的复制
/*************************************************************************
> File Name: 24_ComplexListClone.cpp
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: 2016年08月31日 星期三 14时24分35秒
************************************************************************/ #include <stdio.h>
#include <malloc.h> // 链表结构体
struct ComplexListNode
{
int val;
ComplexListNode* next;
ComplexListNode* rand;
}; // 构造链表
ComplexListNode* createList()
{
ComplexListNode* A = new ComplexListNode();
ComplexListNode* B = new ComplexListNode();
ComplexListNode* C = new ComplexListNode();
ComplexListNode* D = new ComplexListNode();
ComplexListNode* E = new ComplexListNode();
A->val = ;
A->next = B;
A->rand = C;
B->val = ;
B->next = C;
B->rand = E;
C->val = ;
C->next = D;
C->rand = NULL;
D->val = ;
D->next = E;
D->rand = B;
E->val = ;
E->next = NULL;
E->rand = NULL; return A;
} void* PrintComplexList(ComplexListNode* head)
{
while (head)
{
if (head->rand != NULL)
printf("%d rand=%d\n", head->val, head->rand->val);
else
printf("%d\n", head->val);
head = head->next;
}
printf("\n");
} // 复制链表,复制的接在原位置后面
void CloneNodes(ComplexListNode* head)
{
ComplexListNode* node = head;
while (node != NULL)
{
ComplexListNode* newNode = new ComplexListNode();
newNode->val = node->val;
newNode->next = node->next;
newNode->rand = NULL;
node->next = newNode;
node = newNode->next;
}
} // 补充复制的链表的rand指针
void AddRand(ComplexListNode* head)
{
ComplexListNode* node = head;
while (node != NULL)
{
ComplexListNode* newNode = node->next;
if (node->rand != NULL)
newNode->rand = node->rand->next;
node = newNode->next;
}
} // 拆分链表
ComplexListNode* SplitList(ComplexListNode* head)
{
ComplexListNode* node = head;
ComplexListNode* newHead = NULL;
ComplexListNode* newNode = NULL; if (node != NULL)
{
newHead = newNode = node->next;
node->next = newNode->next;
node = node->next;
}
while (node != NULL)
{
newNode->next = node->next;
newNode = newNode->next;
node->next = newNode->next;
node = node->next;
}
return newHead;
} ComplexListNode* Clone(ComplexListNode* head)
{
CloneNodes(head);
AddRand(head);
return SplitList(head);
} int main()
{
ComplexListNode* test = createList();
PrintComplexList(test); ComplexListNode* newTest = Clone(test);
PrintComplexList(test);
PrintComplexList(newTest); }
剑指Offer24 复杂链表的复制的更多相关文章
- 剑指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.(注意,输出结果中请不要返回参数中的节点引用,否 ...
- 剑指offer26 复杂链表的复制
/* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : ...
- 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个距离,遍历 ...
随机推荐
- 微信get post请求到微信服务器 模版 素材操作
1:素材管理 官方文档 package org.konghao.weixin.media; import java.io.File; import java.io.IOException; impor ...
- JavaScript闭包——实现
闭包的官方的解释是:一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 通俗点的说法是: 从理论角度:所有的函数.因为它们都在创建的时候就将上层上下文 ...
- 用一行代码初始化ArrayList
方法1: ArrayList<String> arrList1 = (ArrayList<String>) Arrays.asList("Buenos Aires&q ...
- Simple Worker Thread Class
http://www.codeproject.com/Articles/36184/Simple-Worker-Thread-Class Introduction Many times we need ...
- ARM&Linux 下驱动开发第三节
后台驱动代码如下:比较昨天的,添加了读写指针位置移动操作 #include<linux/init.h> #include<linux/module.h> #include< ...
- Java学习笔记之继承
一.继承的基础 在Java术语中,被继承的类叫超类(superclass)或者父类,继承超类的类叫子类(subclass). 举例说明: class Box { public double width ...
- Codeforces Round #338 (Div. 2) C. Running Track dp
C. Running Track 题目连接: http://www.codeforces.com/contest/615/problem/C Description A boy named Ayrat ...
- MeetMe
- 【JavsScript】JavaScript MVC框架PK:Angular、Backbone、CanJS与Ember
摘要:选择JavaScript MVC框架很难.一方面要考虑的因素非常多,另一方面这种框架也非常多,而要从中选择一个合适的,还真得费一番心思.本文对JavaScript MVC框架Angular.Ba ...
- php或js判断网站访问者来自手机或者pc机
php或js判断网站访问者来自手机或者pc机 2013年9月26日,在弄wtuonline的时候为了区分用户是来自手机版浏览器还是pc,针对不同平台选择不同的网站版本,最终总结如下: ...