复制带随机指针的链表-LintCode
来源:互联网 发布:多核处理器编程 编辑:程序博客网 时间:2024/05/22 08:12
给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点。
返回一个深拷贝的链表。
#ifndef C105_H#define C105_H#include<iostream>using namespace std;struct RandomListNode{ int label; RandomListNode *next, *random; RandomListNode(int x) :label(x), next(NULL), random(NULL) {}};class Solution {public: /** * @param head: The head of linked list with a random pointer. * @return: A new head of a deep copy of the list. */ RandomListNode *copyRandomList(RandomListNode *head) { // write your code here RandomListNode *p = head; RandomListNode *q, *node; while (p != NULL) { RandomListNode *q = new RandomListNode(p->label); q->next = p->next; q->random = p->random; p->next = q; p = q->next; } p = head; while (p != NULL) { q = p->next; if (q->random != NULL) q->random = q->random->next; p = q->next; } p = head; node = p->next; while (p != NULL) { q = p->next; p->next = q->next; p = p->next; if (p != NULL) { q->next = p->next; } } return node; }};#endif
阅读全文
0 0
- LintCode复制带随机指针的链表
- LintCode(105) 复制带随机指针的链表
- LintCode:复制带随机指针的链表
- lintcode,复制带随机指针的链表
- lintcode,复制带随机指针的链表
- LintCode(M) 复制带随机指针的复杂链表
- LintCode 105-复制带随机指针的链表
- Lintcode: 复制带随机指针的链表
- lintcode 复制带随机指针的链表(105)
- 复制带随机指针的链表-LintCode
- [LintCode]105.复制带随机指针的链表
- LintCode:M-复制带随机指针的链表
- LintCode-剑指Offer-(105)复制带随机指针的链表
- [LintCode] 复制带随机指针的链表 Copy List with Random Pointer
- 复制带随机指针的链表
- 复制带随机指针的链表
- 复制带随机指针的链表
- 复制带随机指针的链表
- vue-cli——人员管理实例
- 实时公交到站数计算
- UNITY 开发日记/教程 俄罗斯方块 (四) 方块下落和落地判定
- Hibernate的映射关系与级联(一对一、一对多、多对多)
- Android GreenDao3.0入门学习
- 复制带随机指针的链表-LintCode
- 异步执行SQL
- 小白心酸的JDK安装历程
- windows 下npm Cannot find module 'semver'
- 构造方法能被继承吗
- 深入浅出单实例SINGLETON设计模式
- 关于在编译比特币或者qtum中遇到的一点小问题
- git rebase
- vue-router钩子