Exemple #1
0
        public void GivenAllSameSortedLinkedList_RemoveDuplicates_ShouldReturnCorrectList()
        {
            var head   = new LinkedListBuilder().ConstructFromArray(new int[] { 1, 1, 1 }).Head;
            var result = RemoveDuplicates(head);

            Assert.IsTrue(result.Value == 1 && result.Next == null);
        }
        public void GivenAnotherLinkedList_ReverseFirstKNodes_ShouldReturnCorrectAnswer()
        {
            var head = new LinkedListBuilder().ConstructFromArray(new int[] { 1, 2, 3, 4 }).Head;

            var result = ReverseFirstKNodes(head, 2);

            Assert.IsTrue(result.Value == 2);
        }
        public void GivenLinkedList_SwapNodesInPairs_ShouldReturnCorrectAnswer()
        {
            var head = new LinkedListBuilder().ConstructFromArray(new int[] { 1, 2, 3, 4 }).Head;

            var result = SwapNodesInPairs(head);

            Assert.IsTrue(result.Value == 2);
        }
Exemple #4
0
        public void GivenLinkedListWithNoCycle_HasCycle_ShouldReturnFalse()
        {
            var head = new LinkedListBuilder().ConstructFromArray(new int[] { 1, 2, 3, 4, 5 }).Head;

            var result = HasCycle(head);

            Assert.IsFalse(result);
        }
        public void GivenSortedLinkedList_RemoveDuplicates_ShouldReturnCorrect()
        {
            var head = new LinkedListBuilder().ConstructFromArray(new int[] { 1, 2, 3, 3, 4, 4, 5 }).Head;

            var result = RemoveDuplicates(head);

            Assert.IsTrue(result.Value == 1 && result.Next.Value == 2 && result.Next.Next.Value == 5);
        }
        public void GivenAnotherListAndRotation_RotateList_ShouldReturnCorrectAnswer()
        {
            var head = new LinkedListBuilder().ConstructFromArray(new int[] { 0, 1, 2 }).Head;

            var result = RotateList(head, 4);

            Assert.IsTrue(result.Value == 2 && result.Next.Value == 0 && result.Next.Next.Value == 1);
        }
        public void GivenLinkedList_ConvertToBinaryNumber_ShouldReturnBinaryNumber()
        {
            var head = new LinkedListBuilder().ConstructFromArray(new int[] { 1, 0, 1 }).Head;

            var result = ConvertToBinaryNumber(head);

            Assert.IsTrue(result == 5);
        }
        public void Given312LinkedList_PartitionList_ShouldReturnCorrectList()
        {
            var head = new LinkedListBuilder().ConstructFromArray(new int[] { 3, 1, 2 }).Head;

            var result = Partition(head, 3);

            Assert.IsTrue(
                result.Value == 1 && result.Next.Value == 2 && result.Next.Next.Value == 3);
        }
        public void GivenThirdAnotherLinkedList_PartitionList_ShouldReturnCorrectList()
        {
            var head = new LinkedListBuilder().ConstructFromArray(new int[] { 2, 1 }).Head;

            var result = Partition(head, 2);

            Assert.IsTrue(
                result.Value == 1 && result.Next.Value == 2);
        }
        public void GivenTwoLinkedLists_AddingLinkedList_ShouldReturnTwoLinkedLists()
        {
            var list1 = new LinkedListBuilder().ConstructFromArray(new int[] { 7, 2, 4, 3 }).Head;
            var list2 = new LinkedListBuilder().ConstructFromArray(new int[] { 5, 6, 4 }).Head;

            var result = AddingLinkedListStack(list1, list2);

            Assert.IsTrue(result.Value == 7 && result.Next.Value == 8 && result.Next.Next.Value == 0 && result.Next.Next.Next.Value == 7);
        }
Exemple #11
0
        public void GivenLinkedListInArrayFormAndBinaryTree_()
        {
            var head = new LinkedListBuilder().ConstructFromArray(new int[] { 4, 2, 8 }).Head;
            var root = BinaryTree.CreateBinaryTreeBFS(new string[] { "1", "4", "4", null, "2", "2", null, "1", null, "6", "8", null, null, null, null, "1", "3" });

            var result = IsLinkedListSubPath(head, root);

            Assert.IsTrue(result);
        }
        public void GivenListAndRotation_RotateList_ShouldReturnCorrectAnswer()
        {
            var head = new LinkedListBuilder().ConstructFromArray(new int[] { 1, 2, 3, 4, 5 }).Head;

            var result = RotateList(head, 2);

            Assert.IsTrue(result.Value == 4 && result.Next.Value == 5 && result.Next.Next.Value == 1 &&
                          result.Next.Next.Next.Value == 2 && result.Next.Next.Next.Next.Value == 3);
        }
Exemple #13
0
        public void GivenTwoNonEmptyLSFLinkedListsOfUnevenLength_AddTwoNumbers_Should_ReturnTargetLinkedLists()
        {
            var listNode1 = new LinkedListBuilder().ConstructFromArray(new int[] { 2, 4, 3 }).Head;
            var listNode2 = new LinkedListBuilder().ConstructFromArray(new int[] { 5, 6 }).Head;

            ListNode result = AddTwoNumbers(listNode1, listNode2);

            Assert.IsTrue(result.Value == 7);
            result = result.Next;
            Assert.IsTrue(result.Value == 0);
            result = result.Next;
            Assert.IsTrue(result.Value == 4);
        }
Exemple #14
0
        public void GivenTwoSortedArrays_Merge_ShouldMergeTwoArrays()
        {
            var list1 = new LinkedListBuilder().ConstructFromArray(new int[] { 1, 2, 3 }).Head;
            var list2 = new LinkedListBuilder().ConstructFromArray(new int[] { 1, 1, 2 }).Head;

            ListNode list3 = MergeTwoSortedArrays(list1, list2);

            var result = LinkedListBuilder.GetList(list3);

            Assert.IsTrue(result.Count == 6);
            Assert.IsTrue(result[0] == 1);
            Assert.IsTrue(result[1] == 1);
            Assert.IsTrue(result[2] == 1);
            Assert.IsTrue(result[3] == 2);
            Assert.IsTrue(result[4] == 2);
            Assert.IsTrue(result[5] == 3);
        }