Exemple #1
0
        public static RandomListNode CopyRandomList(RandomListNode head)
        {
            if (head == null)
            {
                return(null);
            }

            var cur = head;

            var nodeMap = new Dictionary <RandomListNode, RandomListNode>();

            while (cur != null)
            {
                nodeMap.Add(cur, new RandomListNode(cur.label));
                cur = cur.next;
            }

            cur = head;
            while (cur != null)
            {
                nodeMap[cur].next   = nodeMap[cur.next];
                nodeMap[cur].random = nodeMap[cur.random];
                cur = cur.next;
            }

            return(nodeMap[head]);
        }
Exemple #2
0
        /// <summary>
        /// 通过数组构建带随机指针的链表
        /// </summary>
        /// <param name="vals"></param>
        /// <returns></returns>
        private static RandomListNode CreateRandomListNodes(int[] vals)
        {
            var dummy    = new RandomListNode(0);
            var nodeList = new List <RandomListNode>();
            var curr     = dummy;

            for (int i = 0; i < vals.Length; i++)
            {
                curr.next = new RandomListNode(vals[i]);
                nodeList.Add(curr.next);
                curr = curr.next;
            }
            nodeList.Add(null);
            curr = dummy;

            var random = new Random();

            while (curr.next != null)
            {
                var rdm = random.Next(0, nodeList.Count);
                curr.next.random = nodeList.ElementAt(rdm);
                curr             = curr.next;
            }

            return(dummy.next);
        }
Exemple #3
0
        public RandomListNode CopyRandomList(RandomListNode head)
        {
            if (head == null)
            {
                return(null);
            }

            //In place copy
            CopyNext(head);
            CopyRandomNode(head);
            return(ExtractCopiedNode(head));
        }
Exemple #4
0
        /// <summary>
        /// 带随机指针的链表打印
        /// </summary>
        /// <param name="node"></param>
        private static void PrintRandomListNode(RandomListNode node)
        {
            RandomListNode curr    = node;
            var            outVals = new List <string>();

            while (curr != null)
            {
                var randomLable = curr.random == null ? "NULL" : curr.random.label.ToString();
                outVals.Add(curr.label + "[" + randomLable + "]");
                curr = curr.next;
            }
            Console.WriteLine(string.Join(" -> ", outVals));
        }
Exemple #5
0
 private void CopyNext(RandomListNode head)
 {
     while (head != null)
     {
         var copyNode = new RandomListNode(head.label)
         {
             next   = head.next,
             random = head.random
         };
         head.next = copyNode;
         head      = head.next.next;
     }
 }
Exemple #6
0
        public static string PrintListNode(RandomListNode head)
        {
            string result = string.Empty;

            while (head != null)
            {
                var randomStr = head.random != null?head.random.val.ToString() : "";

                result = result + head.val.ToString() + "_" + randomStr + "=>";
                head   = head.next;
            }

            return(result);
        }
Exemple #7
0
        private void CopyRandomNode(RandomListNode head)
        {
            while (head != null)
            {
                if (head.next.random != null)
                {
                    // OriginalNode.random.next is the copy of OriginalNode.random
                    // Replace Copied node.random with the copy
                    head.next.random = head.random.next;
                }

                head = head.next.next;
            }
        }
Exemple #8
0
        public RandomListNode CopyRandomList(RandomListNode head)
        {
            RandomListNode iter = head, next;

            // 第1步: 遍历原链表,拷贝每个节点,并将拷贝后的节点插入原先节点之后
            while (iter != null)
            {
                next = iter.next;

                RandomListNode copyNode = new RandomListNode(iter.label);
                iter.next     = copyNode;
                copyNode.next = next;

                iter = next;
            }

            // 第2步: 遍历新链表,将随机节点指针赋值给拷贝后的节点
            iter = head;
            while (iter != null)
            {
                if (iter.random != null)
                {
                    iter.next.random = iter.random.next;
                }
                iter = iter.next.next;
            }

            // 第3步: 复原原链表,并构造新的深拷贝链表
            iter = head;
            RandomListNode pseudoHead = new RandomListNode(0);
            RandomListNode copy, copyIter = pseudoHead;

            while (iter != null)
            {
                next = iter.next.next;

                copy          = iter.next;
                copyIter.next = copy;
                copyIter      = copy;

                iter.next = next;

                iter = next;
            }

            return(pseudoHead.next);
        }
Exemple #9
0
        private RandomListNode ExtractCopiedNode(RandomListNode head)
        {
            RandomListNode r = head.next;

            while (head != null)
            {
                // 1 -> Copy 1 -> 2 -> Copy 2
                var temp = head.next;
                head.next = temp.next;
                head      = head.next;
                if (temp.next != null)
                {
                    temp.next = temp.next.next;
                }
            }

            return(r);
        }
        public RandomListNode CopyRandomList(RandomListNode head)
        {
            if (null == head)
            {
                return(null);
            }
            var dic         = new Dictionary <RandomListNode, RandomListNode>();
            var createdNode = new RandomListNode(head.label);
            var useQueue    = new Queue <RandomListNode>();

            useQueue.Enqueue(head);
            dic.Add(head, createdNode);

            while (useQueue.Count != 0)
            {
                var node    = useQueue.Dequeue();
                var dicNode = dic[node];

                if (null != node.next)
                {
                    useQueue.Enqueue(node.next);
                    if (!dic.ContainsKey(node.next))
                    {
                        dic.Add(node.next, new RandomListNode(node.next.label));
                    }

                    dicNode.next = dic[node.next];
                }

                if (null != node.random)
                {
                    if (!dic.ContainsKey(node.random))
                    {
                        dic.Add(node.random, new RandomListNode(node.random.label));
                    }

                    dicNode.random = dic[node.random];
                }
            }

            return(dic[head]);
        }
Exemple #11
0
        private RandomListNode GetOrAddRandomList(Dictionary <RandomListNode, RandomListNode> dic, RandomListNode node)
        {
            if (node == null)
            {
                return(null);
            }

            if (dic.ContainsKey(node))
            {
                return(dic[node]);
            }
            else
            {
                var newNode = new RandomListNode(node.label);
                dic.Add(node, newNode);

                newNode.next   = GetOrAddRandomList(dic, node.next);
                newNode.random = GetOrAddRandomList(dic, node.random);
                return(newNode);
            }
        }
Exemple #12
0
        // 已知一个复杂的链表,节点中有一个指向本链表任意某个节点的随机指针(可以为null),实现这个链表的深度拷贝
        public static RandomListNode DeepCloneRandomListNode(RandomListNode head)
        {
            RandomListNode tempNode    = null;
            var            nodeToIDMap = new Dictionary <RandomListNode, int>();
            int            id          = 0;

            tempNode = head;
            while (tempNode != null)
            {
                if (!nodeToIDMap.ContainsKey(tempNode))
                {
                    nodeToIDMap.Add(tempNode, id);
                    id++;
                }

                tempNode = tempNode.next;
            }

            var idToNodeMap = new Dictionary <int, RandomListNode>();

            id = 0;
            RandomListNode newHead      = null;
            RandomListNode newNode      = null;
            RandomListNode tempHeadNode = head;

            while (tempHeadNode != null)
            {
                tempNode = new RandomListNode(tempHeadNode.val, null, null);
                if (newHead == null)
                {
                    newHead = tempNode;
                }
                else
                {
                    newNode.next = tempNode;
                }

                newNode = tempNode;
                if (!idToNodeMap.ContainsKey(id))
                {
                    idToNodeMap.Add(id, tempNode);
                    id++;
                }

                tempHeadNode = tempHeadNode.next;
            }

            tempNode = newHead;
            while (tempNode != null && head != null)
            {
                if (head.random == null)
                {
                    tempNode.random = null;
                }
                else
                {
                    var currentId = nodeToIDMap[head.random];
                    tempNode.random = idToNodeMap[currentId];
                }

                head     = head.next;
                tempNode = tempNode.next;
            }

            return(newHead);
        }
Exemple #13
0
 public RandomListNode(int val, RandomListNode next, RandomListNode random)
 {
     this.val    = val;
     this.next   = next;
     this.random = random;
 }
Exemple #14
0
        public RandomListNode CopyRandomList(RandomListNode head)
        {
            var dic = new Dictionary <RandomListNode, RandomListNode>();

            return(GetOrAddRandomList(dic, head));
        }