/*************************************************************************
> 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 复杂链表的复制的更多相关文章

  1. 剑指offer-复杂链表的复制

    题目描述 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否 ...

  2. 剑指Offer——复杂链表的复制

    题目描述: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用, ...

  3. 剑指offer-复杂链表的复制-链表-python

    题目描述 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否 ...

  4. 剑指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 ...

  5. 剑指Offer-25.复杂链表的复制(C++/Java)

    题目: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否则 ...

  6. 用js刷剑指offer(复杂链表的复制)

    题目描述 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否 ...

  7. 剑指offer26 复杂链表的复制

    /* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : ...

  8. 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题要求 ...

  9. 《剑指offer》 链表中倒数第k个节点

    本题来自<剑指offer> 链表中倒数第k个节点 题目: 输入一个链表,输出该链表中倒数第k个结点. 思路: 倒数第k个节点,而且只能访问一遍链表,定义两个节点,两者之间相差k个距离,遍历 ...

随机推荐

  1. web.xml配置详解之listener与context-param

    1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context- ...

  2. JS:公历、农历互转

    先申明这段代码不是我写的,纯粹只是觉的比较好用,所以记录下来以后继续使用,也同样分享给大家,大家有更好的可以推荐给我,谢谢! function CalConv(M, dateStr) { if (da ...

  3. canvas 动态飞速旋转的矩形

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  4. JSON初探

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...

  5. Java练习之最大相同子串

    package string.demo; /* 需求:找到两个字符串的最长共同子串 * 思路: * 1.先看短的那个字符串是否在长的那个字符串中,如果存在,短的那个字符串就是最大共同子串 * 2.如果 ...

  6. 前端优化分析 之 javascript引用位置优化

    在很多优化法则中都提到,尽量将javascript放到页面底部,这是为什么呢 我通过firebug进行了下简单的分析 看下图  本页面首尾都存在javascript代码 我们分析得出 1.整个页面文档 ...

  7. Codeforces Gym 100231F Solitaire 折半搜索

    Solitaire 题目连接: http://codeforces.com/gym/100231/ Description 给你一个8*8棋盘,里面有4个棋子,每个棋子可以做一下某个操作之一: 1.走 ...

  8. cdoj 26 遮挡判断(shadow) 水题

    遮挡判断(shadow) Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/26 ...

  9. 数据库的应用——直接从内存中读取osg节点 (转)

    数据库的应用——直接从内存中读取osg节点 目的:要从数据库中读取节点数据到osg. 一开始的方法是这样的,每当我要添加一个数据库中的节点数据时,首先把它读取到内存中,然后写入一个文件,最后再次从文件 ...

  10. Php-SPL库中的迭代器类详解(转)

    SPL提供了多个迭代器类,分别提供了迭代访问.过滤数据.缓存结果.控制分页等功能.,因为php总是在不断壮大,我尽可能列出SPL中所有的迭代类.下面其中一些迭代器类是需要php5.4,另外一些如Sea ...