Copy List with Random Pointer复制带有随机指针的链表

这道题目很经典,很多书和OJ里都有。如果初次遇到,一定很难搞定。再看其解法,实在是很惊艳。

有两个可以得到深刻启示的地方:

(1)冗余的思想。谈到复制,我们往往都会另起炉灶,而不会原来链表上搞来搞去,感觉很复杂很危险,会一团糟。美错,最危险的地方就是最安全的地方。

(2)指针的步伐。这里的指针有一走两步的操作,很容易导致RE。但是否意味着每步都要仔细考虑指针越界?不然,那样程序会写的很累很乱。还是按照最初的想法,谨慎实现之。回头查看特殊情况,例如空指针情况、只有一个节点的情况,用实际用例去测试,则很容易发现那里会越界。

/**
 * 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) {
        RandomListNode *p,*q,*tmp,*newp;
        if(!head) return nullptr;
        //insert duplicate node
        p=head;
        while(p!=nullptr){
            tmp = new RandomListNode(p->label);
            tmp->next=p->next;
            p->next=tmp;
            p=tmp->next;
        }
        //copy random pointers
        p=head;
        q=p->next;
        while(p!=nullptr && q!=nullptr){
            if(p->random!=nullptr)
               q->random=p->random->next;
            if(q->next==nullptr) break;
            p=q->next;
            q=p->next;

        }
        //disconnect the duplicated
        p=head;
        q=p->next;
        newp=q;
        while(q!=nullptr){
            p->next=q->next;
            p=p->next;
            if(q->next==nullptr) break;
            q->next=p->next;
            q=q->next;
        }

        return newp;

    }
};

测试程序:

#include <iostream>
using namespace std;

struct RandomListNode {
     int label;
	 RandomListNode *next, *random;
     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 };

RandomListNode *copyRandomList(RandomListNode *head) {
        RandomListNode *p,*q,*tmp,*newp;
        if(!head) return nullptr;
        //insert duplicate node
        p=head;
        while(p!=nullptr){
            tmp = new RandomListNode(p->label);
            tmp->next=p->next;
            p->next=tmp;
            p=tmp->next;
        }
        //copy random pointers
        p=head;
        q=p->next;
        while(p!=nullptr && q!=nullptr){
            if(p->random!=nullptr)
               q->random=p->random->next;
            if(q->next==nullptr) break;
            p=q->next;
            q=p->next;

        }
        //disconnect the duplicated
        p=head;
        q=p->next;
        newp=q;
        while(q!=nullptr){
            p->next=q->next;
            p=p->next;
            if(q->next==nullptr) break;
            q->next=p->next;
            q=q->next;
        }

        return newp;

    }

int main() {
	// your code goes here
	int num;
	RandomListNode *tmp, *p ,*head= new RandomListNode(0);
	p=head;
	while(p->label!=-1){
		cin>>num;
		tmp=new RandomListNode(num);
		p->next=tmp;
		p=tmp;
	}

	//p=head;
	//while(p!=nullptr) {cout<<p->label<<" ";p=p->next;}cout<<endl;

	p=copyRandomList(head);
	while(p!=nullptr)
		{cout<<p->label<<" ";p=p->next;}

	return 0;
}
时间: 2024-10-08 03:44:58

Copy List with Random Pointer复制带有随机指针的链表的相关文章

[leetcode]138. Copy List with Random Pointer复制带有随机指针的链表

public RandomListNode copyRandomList(RandomListNode head) { /* 深复制,就是不能只是复制原链表变量,而是做一个和原来链表一模一样的新链表, 每一个节点都是新建的,而不是指向就节点 这个题的难点在于:随机节点. 随机节点有可能指向后边还没有建立的节点,这就没法指. 方法一:一一对应地记录每个新旧节点的映射关系,在常规节点建立后,就去查哈希表,找到对应 新节点的旧节点的random,就是新节点的random */ if (head==nu

[LeetCode] 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. 这道链表的深度拷贝题的难点就在于如何处理随机指针的问题,由于每一个节点都有一个随机指针,这个指针可以为空,也可以指向链表的任意一个节点,如果我们在每生成一个新节点给其随机指

LeetCode138 Copy List with Random Pointer(深度复制带有随机指针的链表) Java题解

题目: 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. 解题: 这题是要复制一个链表,这个链表比普通的链表多一个指针,这个指针可以指向任意地方,可以为空也可以为链表中的任一个节点,今天中午的时候我同学和我说起这题,当时问

【LeetCode】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. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n

Copy List with Random Pointer leetcode java

题目: 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. 题解: 如果要copy一个带有random pointer的list,主要的问题就是有可能这个random指向的位置还没有被copy到,所以解决方法都是多次扫描li

leetcode -day8 Copy List with Random Pointer &amp; Single Number I II

五一中间断了几天,开始继续... 1.  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上的一道题目,分三步进行,首先复制每个链表结点

LeetCode——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. 原题链接:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题目:给定一个链表,当中的每一个节

LeetCode: Copy List with Random Pointer [138]

[题目] 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. [题意] 给定一个链表,每个节点除了next指针外,还有一个random指针,指向任意的节点. 要求,复制这样的一个链表 [思路] 思路1: 先一次生成每个节点对

[Leetcode][JAVA] Clone Graph, Copy List with Random Pointer

Clone Graph: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label a