public void Check_CopyRandomList_BaseCase() { node1 = new NodeWithRadom(7); node2 = new NodeWithRadom(13); node3 = new NodeWithRadom(11); node4 = new NodeWithRadom(10); node5 = new NodeWithRadom(1); node1.next = node2; node1.random = null; node2.next = node3; node2.random = node1; node3.next = node4; node3.random = node5; node4.next = node5; node4.random = node3; node5.next = null; node5.random = node1; var copyRandomListResult1 = solution.CopyRandomList(node1); Assert.IsTrue(copyRandomListResult1.random == null); Assert.IsTrue(copyRandomListResult1.next.random.val == node1.val); }
public void Check_CopyRandomList_ThirdFailedCase() { node1 = new NodeWithRadom(-1); node1.next = null; node1.random = node1; var copyRandomListResult3 = solution.CopyRandomList(node1); Assert.IsTrue(copyRandomListResult3.val == node1.val); Assert.IsTrue(copyRandomListResult3.random.val == node1.val); }
public void Check_CopyRandomList_SecondFailedCase() { node1 = new NodeWithRadom(1); node2 = new NodeWithRadom(2); node1.next = node2; node1.random = node2; node2.next = null; node2.random = node2; var copyRandomListResult2 = solution.CopyRandomList(node1); Assert.IsTrue(copyRandomListResult2.val == node1.val); Assert.IsTrue(copyRandomListResult2.next.val == node2.val); Assert.IsTrue(copyRandomListResult2.next.random.val == node2.val); }
public NodeWithRadom(int _val) { val = _val; next = null; random = null; }
// https://leetcode.com/explore/learn/card/linked-list/213/conclusion/1229/ // Copy List with Random Pointer public NodeWithRadom CopyRandomList(NodeWithRadom head) { if (head == null) { return(head); } var map = new Dictionary <NodeWithRadom, NodeWithRadom>(); var current = head; while (current != null) { map[current] = new NodeWithRadom(current.val); current = current.next; } current = head; while (current != null) { map[current].next = current.next != null ? map[current.next] : null; map[current].random = current.random != null ? map[current.random] : null; current = current.next; } return(map[head]); //IDictionary<int, KeyValuePair<Node, int?>> dictionary = new Dictionary<int, KeyValuePair<Node, int?>>(); //Node curr = head; //int index = 0; //while (curr != null) //{ // int? randomIndex = null; // if (curr.random != null) // { // Node newCurr = head; // randomIndex = 0; // while (newCurr != curr.random) // { // randomIndex++; // newCurr = newCurr.next; // } // } // dictionary.Add(index, KeyValuePair.Create(new Node(curr.val), randomIndex)); // curr = curr.next; // index++; //} //Node newHead = dictionary[0].Key; //if (dictionary[0].Value != null) //{ // var randomIndex = dictionary[0].Value.Value; // newHead.random = dictionary.FirstOrDefault(item => item.Key == randomIndex).Value.Key; //} //Node currNewHead = newHead; //for (int i = 1; i < dictionary.Count(); i++) //{ // currNewHead.next = dictionary[i].Key; // currNewHead = currNewHead.next; // if (dictionary[i].Value != null) // { // var randomIndex = dictionary[i].Value.Value; // currNewHead.random = dictionary.FirstOrDefault(item => item.Key == randomIndex).Value.Key; // } //} //return newHead; }