コード例 #1
0
        public RandomListNode CopyRandomList(RandomListNode head)
        {
            if (head == null)
            {
                return(null);
            }
            Dictionary <RandomListNode, RandomListNode> hm = new Dictionary <RandomListNode, RandomListNode>();
            RandomListNode newHead = new RandomListNode(head.label);

            hm.Add(head, newHead);

            RandomListNode p = head;
            RandomListNode q = newHead;

            p = p.next;

            while (p != null)
            {
                RandomListNode temp = new RandomListNode(p.label);
                hm.Add(p, temp);
                p = p.next;

                q.next = temp;
                q      = temp;
            }


            p = head;
            q = newHead;
            while (p != null)
            {
                if (p.random != null)
                {
                    q.random = hm[p.random];
                }
                else
                {
                    q.random = null;
                }

                p = p.next;
                q = q.next;
            }
            return(hm[head]);
        }
コード例 #2
0
        public void AppendToTaile(int data, int radom_data)
        {
            if (head == null)
            {
                head        = new RandomListNode(data);
                head.next   = null;
                head.random = null;
            }
            else
            {
                RandomListNode n = new RandomListNode(data);
                n.next   = null;
                n.random = null;
                RandomListNode temp = head;
                while (temp.next != null)
                {
                    temp = temp.next;
                }

                temp.next = n;
            }
        }
コード例 #3
0
ファイル: List.cs プロジェクト: xinachilles/Algorithm-List
        /*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.*/



        public RandomListNode CopyRandomList(RandomListNode head)
        {
            if (head == null)
            {
                return(null);
            }
            //map <originalNode, newNode>
            Dictionary <RandomListNode, RandomListNode> mp = new Dictionary <RandomListNode, RandomListNode>();

            RandomListNode res = new RandomListNode(0);
            RandomListNode p   = head;
            RandomListNode q   = res;

            while (p != null)
            {
                RandomListNode tmp = new RandomListNode(p.label);
                q.next = tmp;
                mp[p]  = tmp;
                p      = p.next;
                q      = q.next;
            }
            p = head;
            q = res.next;
            while (p != null)
            {
                if (p.random == null)
                {
                    q.random = null;
                }
                else
                {
                    q.random = mp[p.random];
                }
                p = p.next;
                q = q.next;
            }
            return(res.next);
        }