zoukankan      html  css  js  c++  java
  • [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.

    思考:先进行单链表复制,在定义random指针,每个结点的random指针都要遍历一次链表,时间复杂度为O(n2)。

    /**
     * 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 *copyList(RandomListNode *head)
    	{
    		if(head==NULL) return head;
    		RandomListNode *newhead=NULL;
    		RandomListNode *p=head;
    		RandomListNode *q=newhead;
    		while(p)
    		{
    			RandomListNode *node=new RandomListNode(NULL);
    			node->label=p->label;
    			if(newhead==NULL)
    			{
    				newhead=node;
    				q=node;
    			}
    			else
    			{
    				q->next=node;
    				q=q->next;
    			}
    			p=p->next;
    		}
    		return newhead;
    	}
    	void copyRandomPointer(RandomListNode *head,RandomListNode *newhead)
    	{
    		if(head==NULL||newhead==NULL) return;
    		RandomListNode *p=head;
    		RandomListNode *q=newhead;
    		RandomListNode *a,*b;
    		while(p)
    		{
    			if(p->random==NULL) 
    				q->random==NULL;
    			else
    			{
    				a=head;b=newhead;
    				while(p->random!=a)
    				{
    					a=a->next;
    					b=b->next;
    				}
    				q->random=b;
    			}
    			p=p->next;
    			q=q->next;
    		}
    	}
        RandomListNode *copyRandomList(RandomListNode *head) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            if(head==NULL) return head;
    		RandomListNode *newhead=copyList(head);
    		copyRandomPointer(head,newhead);
    		return newhead;
        }
    };
    

      上述方法,时间都耗费在寻找random指针上。怎样在O(1)时间里找到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:
    	void CloneNodes(RandomListNode *head)
    	{
    		RandomListNode *p=head;
    		while(p)
    		{
    			RandomListNode *node=new RandomListNode(p->label);
    			node->next=p->next;
    			p->next=node;
    			p=node->next;
    		}
    	}
    	void CopyRandomPointer(RandomListNode *head)
    	{
    		RandomListNode *p=head;
    		while(p)
    		{
    			if(p->random==NULL) 
    				p->next->random=NULL;
    			else
    				p->next->random=p->random->next;
    			p=p->next->next;
    		}
    	}
    	RandomListNode *SeparateList(RandomListNode *head)
    	{
    		RandomListNode *p=head;
    		RandomListNode *newhead=NULL;		
    		RandomListNode *q=newhead;
    		while(p)
    		{
    			if(newhead==NULL)
    			{
    				newhead=q=p->next;
    				p->next=q->next;
                    p=p->next;
    			}
    			else
    			{
    				q->next=p->next;
    				q=q->next;
    				p->next=q->next;
    				p=p->next;
    			}
    		}
    		return newhead;
    	}
        RandomListNode *copyRandomList(RandomListNode *head) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            if(head==NULL) return head;
    		CloneNodes(head);
    		CopyRandomPointer(head);		
    		return SeparateList(head);
        }
    };
    

      总感觉百度后好像失去了什么。。学习解题思路,而不是解题方法,切记!

  • 相关阅读:
    超详细JSON解析步骤
    HTTP请求头和响应头总结
    Oracle 数据库常用操作语句大全
    关于HTTP协议,一篇就够了
    PowerDesigner工具建表步骤
    求助:ACM剑气算法
    一道题
    个人Java学习中的遇到的几个模糊的地方
    Java编程:数学黑洞6174
    高级查询
  • 原文地址:https://www.cnblogs.com/Rosanna/p/3427460.html
Copyright © 2011-2022 走看看