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.

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.

参考:深度拷贝,浅度拷贝,Lazy拷贝解析http://www.2cto.com/kf/201401/276086.html

思路: Step 1: 首先指向在原链表的每个节点后面,复制一个新的节点,原链表长度变为 2 倍

random 指针指向的是 原链表节点 random 指针指向的节点的后面的那个节点

Step 2: 将链表拆成两个 lists

\

 

C++实现代码:

#include<iostream>
#include<new>
using namespace std;


// 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==NULL)
            return NULL;
        RandomListNode *p=head;
        while(p)
        {
            RandomListNode *q=new RandomListNode(p->label);
            q->next=p->next;
            p->next=q;
            p=q->next;
        }
        p=head;
        while(p)
        {
            if(p->random)
                p->next->random=p->random->next;
            p=p->next->next;
        }
        RandomListNode *retHead = NULL;
        RandomListNode *tRet = NULL;
        p = head;
        while(p)
        {
            if(retHead == NULL)
            {
                retHead = p->next;
                tRet = retHead;
                p->next = p->next->next;
                p = p->next;
            }
            else
            {
                tRet->next = p->next;
                p->next = p->next->next;
                p = p->next;
                tRet = tRet->next;
            }
        }
        return retHead;
    }
    void createList(RandomListNode *&head)
    {
        RandomListNode *p=NULL;
        int i=0;
        int arr[10]= {10,9,8,7,6,5,4,3,2,1};
        for(i=0; i<10; i++)
        {
            if(head==NULL)
            {
                head=new RandomListNode(arr[i]);
                if(head==NULL)
                    return;
            }
            else
            {
                p=new RandomListNode(arr[i]);
                p->next=head;
                head=p;
            }
        }
    }
};

int main()
{
    Solution s;
    RandomListNode *L=NULL;
    s.createList(L);
    RandomListNode *L2=s.copyRandomList(L);
    RandomListNode *p=L2;
    while(p)
    {
        cout<<p->label<<" ";
        p=p->next;
    }
}

 

相关文章
|
10月前
|
C++
Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的具体讲解在《剑指offer》的P149页有思路讲解,如果你手头有这本书,建议翻阅。
40 0
LeetCode 382. Linked List Random Node
给定一个单链表,随机选择链表的一个节点,并返回相应的节点值。保证每个节点被选的概率一样。
57 0
LeetCode 382. Linked List Random Node
LeetCode 138:复制带随机指针的链表 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.
834 0
|
DataX 程序员 算法
Single linked list by pointer
其实本应该从一般性的表讲起的,先说顺序表,再说链表 。但顺序表的应用范围不是很广,而且说白了就是数组的高级版本,他的优势仅在于两点:1.逻辑直观,易于理解。2.查找某个元素只需要常数时间——O(1),而与此同时,因为每个单元的物理内存都是连续的,所以不便于移动,不便于精细化操作,每次插入和删除都会带来巨额的时间开销。
1031 0
|
Java
java中copy 一个list集合的方法
java将一个list里的数据转移到另外一个list,可以使用for语句,一次使用add方法,示例如下:   ArrayList list1=new ArrayList(); list1.add("1"); list1.
3014 0
[LeetCode] Copy List with Random Pointer
Well, since we need to make a deep copy of the list and nodes in the list have a random pointer that may point to any node in the list (or NULL), we n...
781 0