public void BasicTest()
        {
            var list1        = ListHelpers.CreateLinkedList(1, 2, 3);
            var list2        = ListHelpers.CreateLinkedList(4, 5, 6);
            var intersection = ListHelpers.CreateLinkedList(7, 8, 9);

            AddIntersection(list1, list2, intersection);
            Validate(list1, list2, intersection);

            // Different sized lists
            list1        = ListHelpers.CreateLinkedList(1);
            list2        = ListHelpers.CreateLinkedList(2, 3);
            intersection = ListHelpers.CreateLinkedList(4, 5, 6);
            AddIntersection(list1, list2, intersection);
            Validate(list1, list2, intersection);

            // Intersection is last node
            list1        = ListHelpers.CreateLinkedList(1, 2);
            list2        = ListHelpers.CreateLinkedList(3, 4, 5);
            intersection = ListHelpers.CreateLinkedList(6);
            AddIntersection(list1, list2, intersection);
            Validate(list1, list2, intersection);

            // Repeated values (to ensure references are being computed)
            list1        = ListHelpers.CreateLinkedList(1, 3);
            list2        = ListHelpers.CreateLinkedList(2, 2, 2);
            intersection = ListHelpers.CreateLinkedList(2);
            AddIntersection(list1, list2, intersection);
            Validate(list1, list2, intersection);

            // No intersection
            list1 = ListHelpers.CreateLinkedList(1, 2, 3);
            list2 = ListHelpers.CreateLinkedList(4, 5, 6);
            Validate(list1, list2, null);
        }
Beispiel #2
0
        private static Node <T> AddLoop <T>(Node <T> head, params T[] loop)
            where T : IEquatable <T>
        {
            while (head?.Next != null)
            {
                head = head.Next;
            }

            var loopStart = ListHelpers.CreateLinkedList(loop);
            var loopEnd   = loopStart;

            while (loopEnd.Next != null)
            {
                loopEnd = loopEnd.Next;
            }

            if (head != null)
            {
                head.Next = loopStart;
            }

            loopEnd.Next = loopStart;

            return(loopStart);
        }
        public void BasicTest()
        {
            // Single duplicate
            var list = ListHelpers.CreateLinkedList(1, 2, 2, 3);

            ValidateResult(list, 1, 2, 3);

            // No duplicates
            list = ListHelpers.CreateLinkedList(1, 2, 3);
            ValidateResult(list, 1, 2, 3);

            // Three of the same values in the row
            list = ListHelpers.CreateLinkedList(1, 2, 2, 2, 3);
            ValidateResult(list, 1, 2, 3);

            // Duplicates not next to each other
            list = ListHelpers.CreateLinkedList(1, 2, 3, 2);
            ValidateResult(list, 1, 2, 3);

            // Duplicates next to each other with a third later in the list
            list = ListHelpers.CreateLinkedList(1, 2, 2, 3, 2);
            ValidateResult(list, 1, 2, 3);

            // Several duplicates throughout the list
            list = ListHelpers.CreateLinkedList(1, 2, 2, 3, 2, 4, 3, 5, 5, 6);
            ValidateResult(list, 1, 2, 3, 4, 5, 6);
        }
        public void InvalidInputsTest()
        {
            // Null input
            TestHelpers.AssertExceptionThrown(() => { Question2_2.FindKthToLast <int>(null, 0); }, typeof(ArgumentNullException));

            // K invalid
            var list = ListHelpers.CreateLinkedList(1, 2, 3);

            TestHelpers.AssertExceptionThrown(() => { Question2_2.FindKthToLast(list, -1); }, typeof(ArgumentOutOfRangeException));

            list = ListHelpers.CreateLinkedList(1, 2, 3);
            TestHelpers.AssertExceptionThrown(() => { Question2_2.FindKthToLast(list, 4); }, typeof(ArgumentOutOfRangeException));
        }
        public void EdgeCasesTest()
        {
            // One node
            var list = ListHelpers.CreateLinkedList(1);

            ValidateResult(0, list, 1);

            list = ListHelpers.CreateLinkedList(1);
            ValidateResult(1, list, 1);

            list = ListHelpers.CreateLinkedList(1);
            ValidateResult(2, list, 1);
        }
        public void BasicTest()
        {
            const int NumElements = 5;

            var list = ListHelpers.CreateLinkedList(Enumerable.Range(0, NumElements).Reverse().ToArray());

            for (int i = 0; i < NumElements; i++)
            {
                var input  = ListHelpers.CloneList(list);
                var result = Question2_2.FindKthToLast(input, i);

                Assert.AreEqual(i, result.Data);
            }
        }
        public void EdgeCasesTest()
        {
            // First item duplicated
            var list = ListHelpers.CreateLinkedList(1, 1, 2, 3);

            ValidateResult(list, 1, 2, 3);

            // Last item duplciated
            list = ListHelpers.CreateLinkedList(1, 2, 3, 3);
            ValidateResult(list, 1, 2, 3);

            // List with only one element
            list = ListHelpers.CreateLinkedList(1);
            ValidateResult(list, 1);
        }
Beispiel #8
0
        public void BasicTest()
        {
            // Test case given in the problem
            var demoList = ListHelpers.CreateLinkedList('a', 'b', 'c', 'd', 'e');

            ValidateResult(demoList, demoList.Next.Next, 'a', 'b', 'd', 'e');

            // Remove first node
            var list = ListHelpers.CreateLinkedList(1, 2, 3);

            ValidateResult(list, list, 2, 3);

            // Remove middle node
            list = ListHelpers.CreateLinkedList(1, 2, 3);
            ValidateResult(list, list.Next, 1, 3);
        }
Beispiel #9
0
        public void BasicTest()
        {
            var list      = ListHelpers.CreateLinkedList(1, 2, 3);
            var loopStart = AddLoop(list, 4, 5, 6);

            Validate(list, loopStart);

            // Loop of size one
            list      = ListHelpers.CreateLinkedList(1, 2, 3);
            loopStart = AddLoop(list, 4);
            Validate(list, loopStart);

            // Loop is first item in the list
            loopStart = AddLoop(null, 1, 2, 3);
            Validate(loopStart, loopStart);
        }
        public void BasicTest()
        {
            var list = ListHelpers.CreateLinkedList(3, 2, 1);

            ValidateResult(2, list, 1, 3, 2);

            // No change needed (x is smaller)
            list = ListHelpers.CreateLinkedList(1, 2, 3);
            ValidateResult(0, list, 1, 2, 3);

            // No change needed (x is larger)
            list = ListHelpers.CreateLinkedList(1, 2, 3);
            ValidateResult(4, list, 1, 2, 3);

            list = ListHelpers.CreateLinkedList(1, 3, 3, 1);
            ValidateResult(2, list, 1, 1, 3, 3);
        }
Beispiel #11
0
        private static void Validate(bool expectedResult, params int[] values)
        {
            var head = ListHelpers.CreateLinkedList(values);

            Assert.AreEqual(expectedResult, Question2_6.IsPalindrome(head));
        }
Beispiel #12
0
        public void EdgeCaseTest()
        {
            var list = ListHelpers.CreateLinkedList(1, 2, 3);

            Validate(list, null);
        }
        private static Node <Digit> CreateDigitList(int num, bool reversed = false)
        {
            var result = CreateList(num, reversed);

            return(ListHelpers.CreateLinkedList(result.Select(item => new Digit(item)).ToArray()));
        }