public static Singly intersection(Singly l1, Singly l2) { if (l1 == null || l2 == null) { return(null); } infoList res1 = getTailandSize(l1); infoList res2 = getTailandSize(l2); if (res1.lastNode != res2.lastNode) { return(null); } int diffLists = 0;; if (res1.size > res2.size) { diffLists = res1.size - res2.size; l1 = alignList(l1, diffLists); } else if (res1.size < res2.size) { diffLists = res2.size - res1.size; l2 = alignList(l2, diffLists); } return(findIntersectionNode(l1, l2)); }
public static TheoryData <Singly, Singly> getLists2() { Singly l1 = buildLinkedListNodeFromArray <Singly, int>(new int[2] { 9, 3 }); Singly l2 = buildLinkedListNodeFromArray <Singly, int>(new int[2] { 1, 3 }); Singly l1_1 = buildLinkedListNodeFromArray <Singly, int>(null); Singly l2_2 = buildLinkedListNodeFromArray <Singly, int>(new int[2] { 1, 3 }); Singly l1_3 = buildLinkedListNodeFromArray <Singly, int>(null); Singly l2_3 = buildLinkedListNodeFromArray <Singly, int>(null); Singly l1_4 = buildLinkedListNodeFromArray <Singly, int>(new int[0] { }); Singly l2_4 = buildLinkedListNodeFromArray <Singly, int>(new int[0] { }); return(new TheoryData <Singly, Singly>() { { l1, l2 }, { l1_1, l2_2 }, { l1_3, l2_3 }, { l1_4, l2_4 } }); }
//k=1 last element //k=2 the second to last element public static Singly KLastElement(Singly head, int k) { if (k <= 0) { return(null); } Singly node = head; int i = 0; while (node != null) { i++; node = node.next; } int index = i - k; if (index < 0) { return(null); } node = head; i = 0; while (i != index) { i++; node = node.next; } return(node); }
public static Singly addListsForward(Singly list1, Singly list2) { int len1 = lengthList(list1); int len2 = lengthList(list2); if (len1 > len2) { list2 = padList(list2, len1 - len2); } else if (len1 < len2) { list1 = padList(list1, len2 - len1); } PartialSum res = addLists(list1, list2); Singly r = null; if (res.carry > 0) { r = new Singly(res.carry, null, res.sum); r.data = res.carry; } return(r ?? res.sum); }
public static Singly addListsForwardIte(Singly list1, Singly list2) { int n1 = getNumber(list1); int n2 = getNumber(list2); int sum = n1 + n2; return(buildList(sum)); }
public static void KLastElementRecursiveTest2(Singly head, int k) { Singly node = KLastElementRec(head, k); Assert.True(node.data.Equals(11)); node = KLastElementRec2(head, k); Assert.True(node.data.Equals(11)); }
public static void KLastElementRecursiveTest3(Singly head, int k) { Singly node = KLastElementRec(head, k); Assert.True(node == null); node = KLastElementRec2(head, k); Assert.True(node == null); }
private static void compareValues(Singly list, int[] res) { for (int i = 0; i < res.Length; i++) { Assert.True(list?.data.Equals(res[i])); list = list.next; } }
public static void listLoopTest2(Singly list) { Singly startLoopNode = loopDetection(list); Assert.True(startLoopNode == null); startLoopNode = loopDetection2(list); Assert.True(startLoopNode == null); }
public static void KLastElementTest2(Singly head, int k) { Singly node = KLastElement(head, k); Assert.True(node.data.Equals(11)); Singly node2 = KLastElementTwoPointers(head, k); Assert.True(node2.data.Equals(11)); }
public static void KLastElementTest3(Singly head, int k) { Singly node = KLastElement(head, k); Assert.True(node == null); Singly node2 = KLastElementTwoPointers(head, k); Assert.True(node2 == null); }
private static Singly alignList(Singly l1, int diffLists) { for (int i = 0; i < diffLists; i++) { l1 = l1.next; } return(l1); }
public static void listLoopTest(Singly list) { Singly startLoopNode = loopDetection(list); Assert.True(startLoopNode.data.Equals(44)); startLoopNode = loopDetection2(list); Assert.True(startLoopNode.data.Equals(44)); }
public static TheoryData <Singly, Singly> getLists3() { Singly l1 = buildLinkedListNodeFromArray <Singly, string>(null); Singly l2 = buildLinkedListNodeFromArray <Singly, string>(null); return(new TheoryData <Singly, Singly>() { { l1, l2 } }); }
private static Singly findIntersectionNode(Singly l1, Singly l2) { while (l1 != l2) { l1 = l1.next; l2 = l2.next; } return(l1); }
public static TheoryData <Singly> getListWithoutLoop() { Singly list = buildLinkedListNodeFromArray <Singly, int>(new int[] { 1, 2, 3, 4 }); Singly list2 = buildLinkedListNodeFromArray <Singly, int>(new int[0] { }); return(new TheoryData <Singly>() { list, null, list2 }); }
public static void deleteMiddleNodeTest(Singly middleNode) { Singly newNextNode = middleNode.next.next; Assert.True(middleNode.data.Equals(9)); bool result = DeleteMiddleNode(middleNode); Assert.True(result); Assert.True(middleNode.next == newNextNode); Assert.True(middleNode.data.Equals(-4)); }
//It is not possible to determine if the node passed to the function is the first node of a Singly Linked List public static bool DeleteMiddleNode(Singly middleNode) { if (middleNode == null || middleNode.next == null) { return(false); } Singly nextNode = middleNode.next; middleNode.data = nextNode.data; middleNode.next = nextNode.next; return(true); }
private static int getNumber(Singly list) { int number = 0; while (list != null) { number *= 10; number += list.data; list = list.next; } return(number); }
public static TheoryData <Singly, Singly> getLists2() { Singly l1 = buildLinkedListNodeFromArray <Singly, int>(new int[2] { 8, 1 }); Singly l2 = buildLinkedListNodeFromArray <Singly, int>(new int[0] { }); Singly l2_1 = buildLinkedListNodeFromArray <Singly, int>(null); return(new TheoryData <Singly, Singly>() { { l1, l2 }, { l1, l2_1 } }); }
public static TheoryData <Singly, Singly> getLists() { Singly l1 = buildLinkedListNodeFromArray <Singly, int>(new int[4] { 9, 9, 1, 1 }); Singly l2 = buildLinkedListNodeFromArray <Singly, int>(new int[5] { 9, 8, 8, 1, 1 }); return(new TheoryData <Singly, Singly>() { { l1, l2 } }); }
private static Singly buildList(int num) { Singly result = null; Singly next = null; while (num > 0) { result = new Singly(num % 10, null, next); num /= 10; next = result; } return(result); }
private static Singly padList(Singly l, int padding) { Singly precNode = l; Singly newNode = null; for (int i = 0; i < padding; i++) { newNode = new Singly(); newNode.next = precNode; precNode = newNode; } return(newNode); }
public static TheoryData <Singly> getMiddleNode() { int[] values = { 11, 5, 15, 5, 7, 9, -4 }; Singly node = buildLinkedListNodeFromArray <Singly, int>(values); while (node.data != 9) { node = node.next; } return(new TheoryData <Singly>() { node }); }
private static int lengthList(Singly l) { if (l == null) { return(0); } int count = 1; while (l.next != null) { count++; l = l.next; } return(count); }
private static Singly addListsCarry(Singly list1, Singly list2, int carry) { if (list1 == null && list2 == null && carry == 0) { return(null); } int value = ((list1 != null) ? list1.data : 0) + ((list2 != null) ? list2.data : 0) + carry; Singly res = new Singly(value % 10, null, addListsCarry(list1?.next, list2?.next, value > 9 ? 1 : 0)); return(res); }
private static Result getStartLoopNodeandTail(Singly l, int n) { Result re = new Result(); while (l.next != null) { if (l.data.Equals(n)) { re.startLoop = l; } l = l.next; } re.tail = l; return(re); }
public static TheoryData <Singly> getMiddleNode2() { int[] values = { 11, 5, 15, 5, 7, 9, -4 }; Singly node = buildLinkedListNodeFromArray <Singly, int>(values); Singly tail = node; while (tail.data != -4) { tail = tail.next; } return(new TheoryData <Singly>() { tail, null }); }
private static infoList getTailandSize(Singly l) { if (l == null) { return(null); } int count = 1; while (l.next != null) { count++; l = l.next; } return(new infoList(count, l)); }
public static Singly loopDetection2(Singly list) { Dictionary <Singly, bool> dic = new Dictionary <Singly, bool>(); while (list != null) { if (dic.ContainsKey(list)) { return(list); } dic.Add(list, false); list = list.next; } return(null); }