剑指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个距离,遍历 ...
随机推荐
- oracle 学习笔记--用户管理
oracle 用户管理 创建用户(需要具有dba权限的用户) create user 用户名 identified by 密码 defaule tablespace users //默认表空间 ...
- 常见MFC UI界面库[转]
Xtrme toolkit,BCGControlBar,SkinMagic,AppFace,Skin++,Uskin++,SYGUI,LibUIDK,GuiToolkit,GardenUI等等,除了后 ...
- CSS超出2行省略号
overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-box-orient: vertical; line- ...
- 解决NDK开发中Eclipse报错“Unresolved inclusion jni.h”的最终方法
http://blog.csdn.net/zhubin215130/article/details/39347873
- Hadoop on Mac with IntelliJ IDEA - 8 单表关联NullPointerException
简化陆喜恒. Hadoop实战(第2版)5.4单表关联的代码时遇到空指向异常,经分析是逻辑问题,在此做个记录. 环境:Mac OS X 10.9.5, IntelliJ IDEA 13.1.5, Ha ...
- 转载 C++常用库函数atoi,itoa,strcpy,strcmp的实现
C++常用库函数atoi,itoa,strcpy,strcmp的实现 C语言字符串操作函数 1. 字符串反转 - strRev2. 字符串复制 - strcpy3. 字符串转化为整数 - atoi4. ...
- [CentOS]CentOS/RedHat/Fedora的Proxy设定(yum,wget,,rpm)
yum 「/etc/yum.conf」 proxy=http://proxy.xxx.com:8080/ wget 「/etc/wgetrc」 http_proxy=http://proxy.xxx. ...
- 使用NSData来加载文件
1. Loading Data from Files and URLs // Assuming that there is a text file at /Examples/Test.txt: NSS ...
- AIM Tech Round (Div. 2) D. Array GCD dp
D. Array GCD 题目连接: http://codeforces.com/contest/624/problem/D Description You are given array ai of ...
- C++ Code_StatusBar
主题 1. 创建状态栏 并显示 2. 在状态栏中显示进度条 3. MDI文档显示和隐藏状态栏 4. 5. 代码::创建状态栏 并显示 //手动添加3个ICON //////////////// ...