Example #1
0
        private Node _copy2(Node head)
        {
            if (head == null)
            {
                return(null);
            }

            var dictionary = new Dictionary <Node, Node>();

            Node current = head;

            while (current != null)
            {
                dictionary.Add(current, new Node(current.val));
                current = current.next;
            }

            current = head;
            while (current != null)
            {
                Node copy = dictionary[current];
                if (current.next != null)
                {
                    copy.next = dictionary[current.next];
                }
                if (current.random != null)
                {
                    copy.random = dictionary[current.random];
                }
                current = current.next;
            }

            return(dictionary[head]);
        }
Example #2
0
        public String Test(String input)
        {
            Node head   = Node.Make(JsonConvert.DeserializeObject <Int32?[][]>(input));
            Node result = this.CopyRandomList(head);

            // Assert not equal references
            var instances = new ConditionalWeakTable <Node, Object>();

            Node node = head;

            while (node != null)
            {
                instances.Add(node, Guid.NewGuid());
                node = node.next;
            }

            node = result;
            while (node != null)
            {
                Object  value;
                Boolean foundNode = instances.TryGetValue(node, out value);
                Assert.That(foundNode, Is.False, "Node with label {0} was not copied but a reference to the original one.", node.val);
                if (node.random != null)
                {
                    Boolean foundRandom = instances.TryGetValue(node.random, out value);
                    Assert.That(foundRandom, Is.False, "Node with label {0} was not copied but a reference to the original one.", node.random.val);
                }
                node = node.next;
            }

            return(JsonConvert.SerializeObject(Node.Make(result)));
        }
Example #3
0
        public static Node Make([NotNull] IEnumerable <IEnumerable <Int32?> > pairs)
        {
            if (pairs == null)
            {
                throw new ArgumentNullException(nameof(pairs));
            }

            IEnumerable <Int32?>[] nodes = pairs as IEnumerable <Int32?>[] ?? pairs.ToArray();
            Int32 length = nodes.Length;

            if (length == 0)
            {
                return(null);
            }

            var dictionary = new Dictionary <Int32, Tuple <Node, Int32?> >();

            for (Int32 index = 0; index < length; ++index)
            {
                if (nodes[index] == null)
                {
                    throw new ArgumentException("Node representation cannot be null.", nameof(pairs));
                }

                Int32?[] values = nodes[index] as Int32?[] ?? nodes[index].ToArray();
                if (values.Length != 2)
                {
                    throw new ArgumentException("Node representation cannot have length other than 2.", nameof(pairs));
                }
                if (values[0] == null)
                {
                    throw new ArgumentException("The first value in the Node representation cannot be null.", nameof(pairs));
                }

                dictionary.Add(index, Tuple.Create(new Node(values[0].Value), values[1]));
            }

            Node previous = null;

            for (Int32 index = 0; index < length; ++index)
            {
                Tuple <Node, Int32?> tuple = dictionary[index];

                Node node = tuple.Item1;
                if (previous != null)
                {
                    previous.next = node;
                }
                previous = node;

                Int32?randomIndex = tuple.Item2;
                if (randomIndex != null)
                {
                    node.random = dictionary[randomIndex.Value].Item1;
                }
            }

            return(dictionary[0].Item1);
        }
Example #4
0
        private Node _copy1(Node head)
        {
            Node result = null;

            Int32 index       = 0;
            var   dictionary1 = new Dictionary <Node, Int32>();
            Node  current     = head;

            while (current != null)
            {
                dictionary1.Add(current, index);
                current = current.next;
                index++;
            }

            var dictionary = new Dictionary <Int32, Tuple <Node, Int32?> >();

            index   = 0;
            current = head;
            Node previous = null;

            while (current != null)
            {
                var node = new Node(current.val);
                if (result == null)
                {
                    result = node;
                }
                if (previous != null)
                {
                    previous.next = node;
                }
                dictionary.Add(index, Tuple.Create(node, current.random != null ? dictionary1[current.random] : (Int32?)null));
                previous = node;
                current  = current.next;
                index++;
            }

            foreach (KeyValuePair <Int32, Tuple <Node, Int32?> > pair in dictionary)
            {
                Node  node        = pair.Value.Item1;
                Int32?randomIndex = pair.Value.Item2;
                if (randomIndex != null)
                {
                    node.random = dictionary[randomIndex.Value].Item1;
                }
            }

            return(result);
        }
Example #5
0
        public void Test(String input)
        {
            // ARRANGE
            var values = JsonConvert.DeserializeObject <Int32?[][]>(input);

            // ACT
            Node head = Node.Make(values);
            IEnumerable <IEnumerable <Int32?> > valuesOut = Node.Make(head);

            // ASSERT
            String output = JsonConvert.SerializeObject(valuesOut);

            Assert.That(output, Is.EqualTo(input));
        }
Example #6
0
        public static IEnumerable <IEnumerable <Int32?> > Make(Node node)
        {
            var dictionary = new Dictionary <Node, Int32>();

            Int32 index = 0;

            while (node != null)
            {
                dictionary.Add(node, index++);
                node = node.next;
            }

            return(dictionary.Select(item => new List <Int32?> {
                item.Key.val,
                item.Key.random != null ? dictionary[item.Key.random] : (Int32?)null
            }).ToList());
        }
Example #7
0
        private Node _copy3(Node head)
        {
            if (head == null)
            {
                return(null);
            }

            Node current = head;

            while (current != null)
            {
                Node next = current.next;
                current.next = new Node(current.val)
                {
                    next = next
                };
                current = next;
            }

            current = head;
            while (current != null)
            {
                if (current.next != null)
                {
                    current.next.random = current.random?.next;
                }
                current = current.next?.next;
            }

            current = head;
            Node copy     = head.next;
            Node copyHead = copy;

            while ((current != null) && (copy != null))
            {
                current.next = current.next?.next;
                copy.next    = copy.next?.next;
                current      = current.next;
                copy         = copy.next;
            }

            return(copyHead);
        }
Example #8
0
 public Node CopyRandomList(Node head)
 {
     //return this._copy1(head);
     //return this._copy2(head);
     return(this._copy3(head));
 }