public static IEnumerable <object[]> GetTestCases() { var firstHead1 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(1); var secondHead1 = firstHead1; var expected1 = firstHead1; var expected2 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(1, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(2)); var firstHead2 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(3, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(4, expected2)); var secondHead2 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(5, expected2); var expected3 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(1, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(2, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(3))); var firstHead3 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(3, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(4, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(5, expected3))); var secondHead3 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(6, expected3); var firstHead4 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(1, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(2)); var secondHead4 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(3, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(4)); var expected4 = (CTCI.Ch_02_Linked_Lists.LinkedListNode <int>)null; yield return(new object[] { firstHead1, secondHead1, expected1 }); yield return(new object[] { firstHead2, secondHead2, expected2 }); yield return(new object[] { firstHead3, secondHead3, expected3 }); yield return(new object[] { firstHead4, secondHead4, expected4 }); }
public void GetLoopStart2( CTCI.Ch_02_Linked_Lists.LinkedListNode <int> head, CTCI.Ch_02_Linked_Lists.LinkedListNode <int> expected) { var solution = new DetectLoop(); var actual = solution.GetLoopStart2(head); Assert.Same(expected, actual); }
public void GetIntersectionNode2( CTCI.Ch_02_Linked_Lists.LinkedListNode <int> firstHead, CTCI.Ch_02_Linked_Lists.LinkedListNode <int> secondHead, CTCI.Ch_02_Linked_Lists.LinkedListNode <int> expected) { var solution = new Intersection(); var actual = solution.GetIntersectionNode2(firstHead, secondHead); Assert.Same(expected, actual); }
public static List <T> ToList <T>(CTCI.Ch_02_Linked_Lists.LinkedListNode <T> head) { var list = new List <T>(); var current = head; while (current != null) { list.Add(current.Value); current = current.Next; } return(list); }
public static IEnumerable <object[]> GetTestCases() { var head1 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(1, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(2, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(3, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(4, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(5, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(6)))))); var loopEnd1 = head1.Next.Next.Next.Next.Next; var expected1 = head1.Next.Next; loopEnd1.Next = expected1; var head2 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(1, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(2, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(3, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(4, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(5, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(6)))))); var expected2 = (CTCI.Ch_02_Linked_Lists.LinkedListNode <int>)null; var head3 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(1); var expected3 = head3; expected3.Next = head3; var head4 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(1, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(2, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(3, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(4, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(5, new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(6)))))); var loopEnd4 = head4.Next.Next.Next.Next.Next; var expected4 = head4; loopEnd4.Next = expected4; var head5 = new CTCI.Ch_02_Linked_Lists.LinkedListNode <int>(1); var expected5 = (CTCI.Ch_02_Linked_Lists.LinkedListNode <int>)null; yield return(new object[] { head1, expected1 }); yield return(new object[] { head2, expected2 }); yield return(new object[] { head3, expected3 }); yield return(new object[] { head4, expected4 }); yield return(new object[] { head5, expected5 }); }
private static CTCI.Ch_02_Linked_Lists.LinkedListNode <int> FindByValue(CTCI.Ch_02_Linked_Lists.LinkedListNode <int> head, int value) { var current = head; while (current != null) { if (current.Value == value) { return(current); } current = current.Next; } return(null); }
public static CTCI.Ch_02_Linked_Lists.LinkedListNode <T> FromCollection <T>(IEnumerable <T> collection) { CTCI.Ch_02_Linked_Lists.LinkedListNode <T> head = null; CTCI.Ch_02_Linked_Lists.LinkedListNode <T> previous = null; foreach (var item in collection) { var current = new CTCI.Ch_02_Linked_Lists.LinkedListNode <T>(item); if (previous != null) { previous.Next = current; } previous = current; head ??= current; } return(head); }