コード例 #1
0
        public RandomListNode CopyRandomList(RandomListNode head)
        {
            if (head == null)
                return null;

            Dictionary<RandomListNode, RandomListNode> map = new Dictionary<RandomListNode, RandomListNode>();

            var dummy = new RandomListNode(0);
            var p = head;
            var q = dummy;

            while (p != null)
            {
                q.next = new RandomListNode(p.label);
                map.Add(p, q.next);
                q = q.next;
                p = p.next;
            }

            p = head;
            q = dummy;

            while (p != null)
            {
                if (p.random != null)
                    q.next.random = map[p.random];

                p = p.next;
                q = q.next;
            }

            return dummy.next;
        }
コード例 #2
0
        public RandomListNode copyRandomList(RandomListNode head)
        {
            if (head == null)
            {
                return(null);
            }

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

            RandomListNode orig = head;

            while (orig != null)
            {
                var cpy = new RandomListNode(orig.label);
                map.Add(orig, cpy);
                orig = orig.next;
            }

            orig = head;
            while (orig != null)
            {
                var cpy = map[orig];
                cpy.next   = orig.next == null ? null : map[orig.next];
                cpy.random = orig.random == null ? null : map[orig.random];
                orig       = orig.next;
            }

            return(map[head]);
        }
コード例 #3
0
    public RandomListNode CopyRandomList(RandomListNode head)
    {
        if (head == null)
        {
            return(null);
        }
        var temp = head;

        while (temp != null)
        {
            RandomListNode n = new RandomListNode(temp.label);
            n.next    = temp.next;
            temp.next = n;
            temp      = n.next;
        }
        temp = head;
        while (temp != null)
        {
            if (temp.random != null)
            {
                temp.next.random = temp.random.next;
            }
            temp = temp.next.next;
        }
        temp = head;
        var answer = head.next;

        while (temp.next != null)
        {
            var n = temp.next;
            temp.next = n.next;
            temp      = n;
        }
        return(answer);
    }
コード例 #4
0
    public RandomListNode CopyRandomList(RandomListNode head)
    {
        RandomListNode result = new RandomListNode(-1);

            RandomListNode temp = result;
            RandomListNode headcache = head;

            Hashtable oldmap = new Hashtable();		// the hashtable with mapping pair ( key = original node, value = index of that node)
            Hashtable newmap = new Hashtable();		// the hashtable with mapping pair ( key = index of the original node, value = the new deep copied node)

            result.next = null;						// initialization
            result.random = null;					// initialization

            if (head == null) return result.next;

            int count = 1;							// counter for the index of the node

            /* The while loop that performs deep copy with next reference only*/
            while (head != null)
            {

                RandomListNode tnode = new RandomListNode(head.label);

                tnode.next = null;
                tnode.random = null;

                temp.next = tnode;

                oldmap.Add(head, count);	// add the mapping pair ( key = original node, value = index of that node)
                newmap.Add(count, tnode);	// add the mapping pair ( key = index of the original node, value = the new deep copied node)

                head = head.next;
                temp = temp.next;

                count++;
            }

            temp = result.next;				// back to the new nodes' head
            head = headcache;				// back to the pld nodes' head

            while (temp != null)
            {

                if (head.random == null)
                {
                    temp.random = null;
                }
                else
                {

                    temp.random = newmap[oldmap[head.random]] as RandomListNode;	// get the desired node from the hashtable

                }

                temp = temp.next;
                head = head.next;
            }

            return result.next;
    }
コード例 #5
0
ファイル: Program.cs プロジェクト: htlsmile/LeetCode
        static void Test(RandomListNode head)
        {
            var solution = new Solution();
            var result   = solution.CopyRandomList(head);

            ShowRandomListNode(result);
        }
コード例 #6
0
    ///<summery>Copies a list of nodes. Generates a map of a key node and a new value node with the key nodes value
    /// Uses that mapping to update the key nodes with the correct values for next and random</summery>
    /// <param name="head">The head of the Linked List of random nodes</param>
    /// <returns> The head of the copied list</returns>
    public RandomListNode CopyRandomList(RandomListNode head)
    {
        // Dir of Nodes and Nodes. Key<Random Node>:Value<Copy of Random Node value>
        Dictionary <RandomListNode, RandomListNode> map = new Dictionary <RandomListNode, RandomListNode>();
        RandomListNode nHead = head;

        if (head != null)
        {
            // Build a map of Nodes and node copies
            while (nHead != null)
            {
                map.Add(nHead, new RandomListNode(nHead.label));
                nHead = nHead.next;
            }

            // make newHead be the copied random node value
            RandomListNode newHead = map[head];
            // Loop over all key:values in map to update the copied node
            foreach (KeyValuePair <RandomListNode, RandomListNode> pair in map)
            {
                pair.Value.label = pair.Key.label;
                // If the node to copy has a next set next to the copied node otherwise the copy points to null
                pair.Value.next = pair.Key.next != null ? map[pair.Key.next] : null;
                // If the node to copy has a random set random to the copied node otherwise the copy points to null
                pair.Value.random = pair.Key.random != null ? map[pair.Key.random] : null;
            }
            return(newHead);
        }
        else
        {
            return(null);
        }
    }
コード例 #7
0
    public RandomListNode CopyRandomList(RandomListNode head)
    {
        if (head == null)
        {
            return(null);
        }
        var dict     = new Dictionary <RandomListNode, RandomListNode>();
        var currNode = head;

        // put <original, copied> pair into dict
        while (currNode != null)
        {
            dict.Add(currNode, new RandomListNode(currNode.label));
            currNode = currNode.next;
        }

        currNode = head;
        var copiedHead = dict[currNode];

        while (currNode != null)
        {
            var copiedNode = dict[currNode];
            copiedNode.next   = currNode.next != null ? dict[currNode.next] : null;
            copiedNode.random = currNode.random != null ? dict[currNode.random] : null;

            currNode = currNode.next;
        }

        return(copiedHead);
    }
コード例 #8
0
        public static RandomListNode Clone(RandomListNode head)
        {
            if (head == null)
            {
                return(null);
            }
            var dic = new Dictionary <RandomListNode, RandomListNode>();

            var oldNode = head;
            var newNode = new RandomListNode(head.Val);

            dic.Add(oldNode, newNode);

            //每次oldnode 都被赋值成oldnode.next,总会到null的
            while (oldNode != null)
            {
                newNode.Random = Get(oldNode.Random, dic);
                newNode.Next   = Get(oldNode.Next, dic);

                //两个指针都往下跑
                oldNode = oldNode.Next;
                newNode = newNode.Next;
            }
            return(dic[head]);
        }
コード例 #9
0
ファイル: ListOperation.cs プロジェクト: SunTaoA/Algorithm
        public RandomListNode Clone(RandomListNode pHead)
        {
            if (pHead == null)
            {
                return(null);
            }

            RandomListNode cur = null;

            cur       = new RandomListNode(pHead.label);
            cloneHead = cur;
            if (pHead.random != null)
            {
                cur.random = new RandomListNode(pHead.random.label);
            }

            pHead = pHead.next;
            while (pHead != null)
            {
                cur.next = new RandomListNode(pHead.label);
                if (pHead.random != null)
                {
                    cur.next.random = new RandomListNode(pHead.random.label);
                }

                pHead = pHead.next;
                cur   = cur.next;
            }

            return(cloneHead);
        }
コード例 #10
0
        private RandomListNode BuildList()
        {
            var head  = new RandomListNode(0);
            var node1 = new RandomListNode(1);
            var node2 = new RandomListNode(2);
            var node3 = new RandomListNode(3);
            var node4 = new RandomListNode(4);
            var node5 = new RandomListNode(5);

            head.Next   = node1;
            head.Random = node2;

            node1.Next   = node2;
            node1.Random = null;

            node2.Next   = node3;
            node2.Random = node5;

            node3.Next   = node4;
            node3.Random = node3;

            node4.Next   = node5;
            node5.Random = node2;


            return(head);
        }
コード例 #11
0
    public RandomListNode CopyRandomList(RandomListNode head)
    {
        RandomListNode result = new RandomListNode(-1);

        RandomListNode temp      = result;
        RandomListNode headcache = head;

        Hashtable oldmap = new Hashtable();             // the hashtable with mapping pair ( key = original node, value = index of that node)
        Hashtable newmap = new Hashtable();             // the hashtable with mapping pair ( key = index of the original node, value = the new deep copied node)

        result.next   = null;                           // initialization
        result.random = null;                           // initialization

        if (head == null)
        {
            return(result.next);
        }

        int count = 1;                                                          // counter for the index of the node

        /* The while loop that performs deep copy with next reference only*/
        while (head != null)
        {
            RandomListNode tnode = new RandomListNode(head.label);

            tnode.next   = null;
            tnode.random = null;

            temp.next = tnode;

            oldmap.Add(head, count);            // add the mapping pair ( key = original node, value = index of that node)
            newmap.Add(count, tnode);           // add the mapping pair ( key = index of the original node, value = the new deep copied node)

            head = head.next;
            temp = temp.next;

            count++;
        }

        temp = result.next;                             // back to the new nodes' head
        head = headcache;                               // back to the pld nodes' head

        while (temp != null)
        {
            if (head.random == null)
            {
                temp.random = null;
            }
            else
            {
                temp.random = newmap[oldmap[head.random]] as RandomListNode;            // get the desired node from the hashtable
            }

            temp = temp.next;
            head = head.next;
        }

        return(result.next);
    }
コード例 #12
0
            public RandomListNode ReconnectNode(RandomListNode pHead)
            {
                RandomListNode cloneHead, cloneNode, node;

                if (node != null)
                {
                }
            }
コード例 #13
0
 private void AssertList(RandomListNode head, params int[] nums)
 {
     for (int i = 0; i < nums.Length; i++)
     {
         Assert.AreEqual(nums[i], head.label);
         head = head.next;
     }
     Assert.IsNull(head);
 }
コード例 #14
0
 private void GenerateAllNodesAndMapping(RandomListNode head, Dictionary <RandomListNode, RandomListNode> dict)
 {
     while (head != null)
     {
         RandomListNode node = new RandomListNode(head.label);
         dict.Add(head, node);
         head = head.next;
     }
 }
コード例 #15
0
 RandomListNode GetCopyNode(RandomListNode node)
 {
     if (!table.ContainsKey(node.label))
     {
         var newCopy = new RandomListNode(node.label);
         table[node.label] = newCopy;
     }
     return(table[node.label]);
 }
コード例 #16
0
    public RandomListNode CopyRandomList(RandomListNode head)
    {
        if (head == null)
        {
            return(null);
        }
        Dictionary <int, RandomListNode> cache = new Dictionary <int, RandomListNode>();

        return(Clone(head, cache));
    }
コード例 #17
0
 public RandomListNode Clone(RandomListNode pHead)
 {
     // write code here
     if (pHead == null)
     {
         return(null);
     }
     CloneNodes(pHead);
     CloneRandom(pHead);
     return(ReConnectNode(pHead));
 }
コード例 #18
0
        public RandomListNode CopyRandomListTwo(RandomListNode head)
        {
            if (head == null)
            {
                return(null);
            }

            CopyNext(head);
            CopyRandom(head);
            return(SplitList(head));
        }
コード例 #19
0
        private void CopyRandom(RandomListNode head)
        {
            while (head != null)
            {
                if (head.next.random != null)
                {
                    head.next.random = head.random.next;
                }

                head = head.next.next;
            }
        }
コード例 #20
0
        public RandomListNode Clone(RandomListNode pHead)
        {
            // write code here
            if (pHead == null)
            {
                return(pHead);
            }
            List <RandomListNode> randomList = new List <RandomListNode>();
            List <RandomListNode> sourceList = new List <RandomListNode>();
            RandomListNode        num        = pHead;
            int count = 0;

            while (num != null)
            {
                sourceList.Add(num);
                num = num.next;
                count++;
            }
            RandomListNode temp = pHead;
            Random         a    = new Random();

            while (temp != null)
            {
                int            ran    = a.Next(-1, count);
                RandomListNode random = pHead;
                if (ran == -1)
                {
                    randomList.Add(null);
                }
                else
                {
                    for (int i = 0; i < ran; i++)
                    {
                        random = random.next;
                        randomList.Add(random);
                    }
                }
                temp.random = random;
                temp        = temp.next;
            }
            RandomListNode head = new RandomListNode(sourceList[0].label);

            head.random = randomList[0];
            RandomListNode newTemp = head;

            for (int i = 1; i < sourceList.Count; i++)
            {
                newTemp.next   = sourceList[i];
                newTemp.random = randomList[i];
                newTemp        = newTemp.next;
            }
            return(head);
        }
コード例 #21
0
        public RandomListNode CopyRandomList(RandomListNode head)
        {
            if (head == null)
            {
                return(null);
            }

            var p = head;

            // Create a copy of the node after each node.
            RandomListNode q;

            while (p != null)
            {
                q = new RandomListNode(p.label)
                {
                    next = p.next
                };

                p.next = q;
                p      = p.next.next;
            }

            // Fix the random point
            p = head;
            while (p != null)
            {
                if (p.random != null)
                {
                    p.next.random = p.random.next;
                }

                p = p.next.next;
            }

            // break the link
            p = head;
            var newhead = p.next;

            q = newhead;
            while (p != null)
            {
                p.next = p.next.next;
                p      = p.next;
                if (p != null)
                {
                    q.next = p.next;
                    q      = q.next;
                }
            }

            return(newhead);
        }
コード例 #22
0
    public RandomListNode CopyRandomList(RandomListNode head)
    {
        if (head == null)
        {
            return(null);
        }
        Dictionary <RandomListNode, RandomListNode> dict = new Dictionary <RandomListNode, RandomListNode>();

        GenerateAllNodesAndMapping(head, dict);
        CopyList(head, dict);
        return(dict[head]);
    }
コード例 #23
0
            public void CloneNodes(RandomListNode phead)
            {
                RandomListNode pNode = phead;

                while (pNode != null)
                {
                    RandomListNode pClone = new RandomListNode(pNode.label);
                    pClone.next = pNode.next;
                    pNode.next  = pClone;
                    pNode       = pClone.next;
                }
            }
コード例 #24
0
    private RandomListNode CopyNode(RandomListNode node, Dictionary <RandomListNode, RandomListNode> map)
    {
        if (node == null)
        {
            return(null);
        }

        if (!map.ContainsKey(node))
        {
            map.Add(node, new RandomListNode(node.label));
        }
        return(map[node]);
    }
コード例 #25
0
 private void CopyNext(RandomListNode head)
 {
     while (head != null)
     {
         var newNode = new RandomListNode(head.label)
         {
             next   = head.next,
             random = head.random
         };
         head.next = newNode;
         head      = head.next.next;
     }
 }
コード例 #26
0
            public void CloneRandom(RandomListNode pHead)
            {
                RandomListNode pNode = pHead;

                while (pNode != null)
                {
                    RandomListNode pClone = pNode.next;
                    if (pNode.random != null)
                    {
                        pClone.random = pNode.random.next;
                    }
                    pNode = pClone.next;
                }
            }
コード例 #27
0
            private static void CloneRandom(RandomListNode pHead)
            {
                RandomListNode node = pHead;

                while (node != null)
                {
                    RandomListNode clone = node.next;
                    if (node.random != null)
                    {
                        clone.random = (node.random).next;
                    }
                    node = clone.next;
                }
            }
コード例 #28
0
ファイル: Solution.cs プロジェクト: TenantTim/LeetCode
        public RandomListNode CopyRandomList(RandomListNode head)
        {
            if (head == null)
            {
                return(null);
            }

            Dictionary <RandomListNode, RandomListNode> m_dupMap = new Dictionary <RandomListNode, RandomListNode>();

            RandomListNode oldIter = head, oldHead = head;
            RandomListNode newIter = null, newHead = new RandomListNode(head.label);

            while (oldIter != null)
            {
                if (newIter == null)
                {
                    newIter = newHead;
                }

                m_dupMap[oldIter] = newIter;

                if (oldIter.next != null)
                {
                    newIter.next = new RandomListNode(oldIter.next.label);
                    newIter      = newIter.next;
                }

                oldIter = oldIter.next;
            }

            oldIter = oldHead;
            newIter = newHead;

            while (oldIter != null)
            {
                if (oldIter.random == null)
                {
                    newIter.random = null;
                }
                else
                {
                    newIter.random = m_dupMap[oldIter.random];
                }

                oldIter = oldIter.next;
                newIter = newIter.next;
            }

            return(newHead);
        }
コード例 #29
0
        public RandomListNode CopyRandomList(RandomListNode head)
        {
            var node = head;

            //insert copy of each node after each node
            while (node != null)
            {
                var c    = new RandomListNode(node.label);
                var next = node.next;
                node.next      = c;
                node.next.next = next;
                node           = node.next.next;
            }

            //go through each real node and get random
            //find random copy(as next node) and set to copy.random
            node = head;
            while (node != null)
            {
                var r = node.random;
                if (r != null)
                {
                    node.next.random = node.random.next;
                }
                node = node.next.next;
            }

            //extract copies node to separate list and clean up initial list
            node = head;
            RandomListNode copy  = null;
            RandomListNode cnode = null;

            while (node != null)
            {
                if (copy == null)
                {
                    copy  = node.next;
                    cnode = copy;
                }
                else
                {
                    cnode.next = node.next;
                    cnode      = cnode.next;
                }
                node.next = node.next.next;
                node      = node.next;
            }
            return(copy);
        }
コード例 #30
0
            private static void CloneNodes(RandomListNode pHead)
            {
                RandomListNode node = pHead;

                while (node != null)
                {
                    RandomListNode Clonenode = new RandomListNode(0);
                    Clonenode.label  = node.label;
                    Clonenode.next   = node.next;
                    Clonenode.random = null;

                    node.next = Clonenode;
                    node      = Clonenode.next;
                }
            }
コード例 #31
0
            RandomListNode FindNode(RandomListNode head, int label)
            {
                var cursor = head;

                while (cursor != null)
                {
                    if (cursor.label == label)
                    {
                        return(cursor);
                    }
                    cursor = cursor.next;
                }

                return(null);
            }
コード例 #32
0
            /// <summary>
            /// 这种思路有些问题
            /// </summary>
            /// <param name="pHead"></param>
            /// <returns></returns>
            public RandomListNode ReConnectNode(RandomListNode pHead)
            {
                RandomListNode cloneHead, cloneNode, node;

                node      = pHead;
                cloneHead = cloneNode = node.next;
                while (cloneHead.next != null)
                {
                    node           = cloneHead.next;
                    cloneHead.next = node.next;
                    cloneHead      = cloneHead.next;
                }
                //cloneHead = pHead.next;
                return(cloneNode);
            }
コード例 #33
0
        public static void Run()
        {
            var st = new Solution138();


            Console.WriteLine("Start: {0}", DateTime.Now);
            RandomListNode p = new RandomListNode(1);
            RandomListNode p2 = new RandomListNode(2);
            var p3 = new RandomListNode(3);

            p.next = p2;
            p2.next = p3;
            p3.next = null;
            p.random = p3;
            p2.random = p3;
            p3.random = p3;

            var q = st.CopyRandomList(p);

            Console.WriteLine("End: {0}", DateTime.Now);
        }
コード例 #34
0
    public RandomListNode CopyRandomList(RandomListNode head) {
        var dict = new Dictionary<int, Tuple<int?, int?>>();
        var current = head;
        while (current != null)
        {
            dict.Add(current.label, Tuple.Create(current.next == null ? (int?) null : current.next.label, current.random == null ? (int?) null : current.random.label));
            current = current.next;
        }

        var dict2 = new Dictionary<int, RandomListNode>();
        foreach (var label in dict.Keys)
        {
            dict2.Add(label, new RandomListNode(label));
        }
        foreach (var pair in dict)
        {
            var next = pair.Value.Item1;
            if (next.HasValue) dict2[pair.Key].next = dict2[next.Value];
            var random = pair.Value.Item2;
            if (random.HasValue) dict2[pair.Key].random = dict2[random.Value];
        }

        return head == null ? null : dict2[head.label];
    }