LeetCode-138.Copy List with Random Pointer
2016-07-02 21:05
288 查看
https://leetcode.com/problems/copy-list-with-random-pointer/
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.(《剑指Offer》P147 / 复杂链表的复制)
先忽略random节点,复制原始链表,并将每个原始节点和新复制的节点存放入map。然后通过遍历map,匹配其random节点
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head)
{
if (!head)
return NULL;
unordered_map<RandomListNode*, RandomListNode*> table;
RandomListNode* pHead = head;
RandomListNode* headCopy = new RandomListNode(pHead->label);
table[pHead] = headCopy;
while (pHead->next)
{
RandomListNode* node = new RandomListNode(pHead->next->label);
headCopy->next = node;
table[pHead->next] = node;
pHead = pHead->next;
headCopy = headCopy->next;
}
for (auto node : table)
{
if (node.first->random)
node.second->random = table[node.first->random];
}
return table[head];
}
};
不用map
1、复制每个节点,如:复制节点A得到A1,将A1插入节点A后面
2、遍历链表,A1->random = A->random->next;
3、将链表拆分成原链表和复制后的链表
RandomListNode *copyRandomList(RandomListNode *head)
{
if (!head)
return NULL;
RandomListNode* pHead = head, *node;
while (head)//1->1->2->2->3->3->4->4...
{
node = new RandomListNode(head->label);
node->next = head->next;
head->next = node;
head = node->next;
}
head = pHead;
while (head)
{
if (head->random)
head->next->random = head->random->next;
head = head->next->next;
}
head = pHead;
RandomListNode* res = head->next;
while (head->next)
{
node = head->next;
head->next = node->next;
head = node;
}
return res;
}
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.(《剑指Offer》P147 / 复杂链表的复制)
先忽略random节点,复制原始链表,并将每个原始节点和新复制的节点存放入map。然后通过遍历map,匹配其random节点
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head)
{
if (!head)
return NULL;
unordered_map<RandomListNode*, RandomListNode*> table;
RandomListNode* pHead = head;
RandomListNode* headCopy = new RandomListNode(pHead->label);
table[pHead] = headCopy;
while (pHead->next)
{
RandomListNode* node = new RandomListNode(pHead->next->label);
headCopy->next = node;
table[pHead->next] = node;
pHead = pHead->next;
headCopy = headCopy->next;
}
for (auto node : table)
{
if (node.first->random)
node.second->random = table[node.first->random];
}
return table[head];
}
};
不用map
1、复制每个节点,如:复制节点A得到A1,将A1插入节点A后面
2、遍历链表,A1->random = A->random->next;
3、将链表拆分成原链表和复制后的链表
RandomListNode *copyRandomList(RandomListNode *head)
{
if (!head)
return NULL;
RandomListNode* pHead = head, *node;
while (head)//1->1->2->2->3->3->4->4...
{
node = new RandomListNode(head->label);
node->next = head->next;
head->next = node;
head = node->next;
}
head = pHead;
while (head)
{
if (head->random)
head->next->random = head->random->next;
head = head->next->next;
}
head = pHead;
RandomListNode* res = head->next;
while (head->next)
{
node = head->next;
head->next = node->next;
head = node;
}
return res;
}
相关文章推荐
- [C/C++]反转链表
- C#实现基于链表的内存记事本实例
- C#模拟链表数据结构的实例解析
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- C++利用静态成员或类模板构建链表的方法讲解
- C++实现简单的学生管理系统
- Linux内核链表实现过程
- C++链表倒序实现方法
- C#通过链表实现队列的方法
- Node.js环境下JavaScript实现单链表与双链表结构
- C#实现的简单链表类实例
- 找出链表倒数第n个节点元素的二个方法
- Java数据结构之简单链表的定义与实现方法示例
- Java模拟有序链表数据结构的示例
- C语言单循环链表的表示与实现实例详解
- C++实现的链表类实例
- PHP小教程之实现链表
- PHP中模拟链表和链表的基本操作示例
- C语言双向链表的表示与实现实例详解
- js链表操作(实例讲解)