예제 #1
0
        public void MiddleNodeTest_2()
        {
            int[]    nodes = { 1, 2, 3, 4, 5, 6 };
            ListNode list  = ListNodeHelper.CreateLinkedListByArray(nodes);

            Assert.IsTrue(4 == t867.MiddleNode(list).val);
        }
        public void IsPalindromeTest_5()
        {
            int[]    nodes = { 1, 2, 3, 4, 5, 3, 2, 1 };
            ListNode head  = ListNodeHelper.CreateLinkedListByArray(nodes);

            Assert.IsTrue(false == t190.IsPalindrome(head));
        }
        public void RemoveElementsTest_5()
        {
            int[]    nodes  = { 6, 6, 6, 6, 6, 6 };
            ListNode lst    = ListNodeHelper.CreateLinkedListByArray(nodes);
            ListNode result = t190.RemoveElements(lst, 6);

            Assert.IsTrue(result == null);
        }
예제 #4
0
        public void DeleteDuplicatesTest_null()
        {
            ListNode head = null;

            t83.DeleteDuplicates(head);
            int[] result = ListNodeHelper.GetElementsFromLinkedList(head);
            int[] expect = { };
            Assert.IsTrue(CompareHelper.CompareArrays(result, expect));
        }
예제 #5
0
        public void Test_Generic(string listStr, int expected)
        {
            var head = ListNodeHelper.BuildList(listStr);

            var sol = new Solution();
            var res = sol.PairSum(head);

            Assert.AreEqual(res, expected);
        }
예제 #6
0
        public void Test_Generic(string listStr, int k, string expectedStr)
        {
            var head     = ListNodeHelper.BuildList(listStr);
            var sol      = new Solution();
            var res      = sol.SplitListToParts(head, k);
            var expected = ListNodeHelper.BuildListArray(expectedStr);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expected));
        }
예제 #7
0
        public static string Run(string left = "1 3 5 7 9", string right = "0 2 4 6 8")
        {
            ListNode leftLink  = ListNodeHelper.StringToListNode(left);
            ListNode rightLink = ListNodeHelper.StringToListNode(right);

            ListNode result = MergeTwoLists(leftLink, rightLink);

            return(ListNodeHelper.ListNodeToString(result));
        }
예제 #8
0
        public void DeleteDuplicatesTest_single()
        {
            int[]    list = { 1 };
            ListNode head = ListNodeHelper.CreateLinkedListByArray(list);

            t83.DeleteDuplicates(head);
            int[] result = ListNodeHelper.GetElementsFromLinkedList(head);
            int[] expect = { 1 };
            Assert.IsTrue(CompareHelper.CompareArrays(result, expect));
        }
예제 #9
0
        public void Test_Generic(string inputs, int val, string expectedStr)
        {
            var head = ListNodeHelper.BuildList(inputs);
            var sol  = new Solution();
            var res  = sol.RemoveElements(head, val);

            var expected = ListNodeHelper.BuildList(expectedStr);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expected));
        }
예제 #10
0
        public void Test_GenericStr(string listStr, string expectedStr)
        {
            var head     = ListNodeHelper.BuildList(listStr);
            var expected = ListNodeHelper.BuildList(expectedStr);

            var sol = new Solution();
            var res = sol.DeleteDuplicates(head);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expected));
        }
예제 #11
0
        public void Test_Generic(string inputStr, string expectedStr)
        {
            var head     = ListNodeHelper.BuildList(inputStr);
            var expected = ListNodeHelper.BuildList(expectedStr);

            var sol = new Solution();
            var res = sol.DeleteMiddle(head);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expected));
        }
        public void RemoveElementsTest_6()
        {
            int[]    nodes  = { 1, 2, 2, 1 };
            ListNode lst    = ListNodeHelper.CreateLinkedListByArray(nodes);
            ListNode result = t190.RemoveElements(lst, 2);

            int[] resultArr = ListNodeHelper.GetElementsFromLinkedList(result);
            int[] expect    = { 1, 1 };
            Assert.IsTrue(CompareHelper.CompareArrays(expect, resultArr));
        }
예제 #13
0
        public void Test_Generic_O1(string listStr, int loopIdx, bool expected)
        {
            var head = ListNodeHelper.BuildList(listStr);

            MakeLoop(head, loopIdx);

            var sol = new Solution_O1();
            var res = sol.HasCycle(head);

            Assert.AreEqual(res, expected);
        }
예제 #14
0
        public void Test_Generic(string l1Str, string l2Str, string expectedStr)
        {
            var l1       = ListNodeHelper.BuildList(l1Str);
            var l2       = ListNodeHelper.BuildList(l2Str);
            var expected = ListNodeHelper.BuildList(expectedStr);

            var sol = new Solution();
            var res = sol.AddTwoNumbers(l1, l2);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expected));
        }
예제 #15
0
        public void Test_Generic(string strList1, int a, int b, string strList2, string expected)
        {
            var list1        = ListNodeHelper.BuildList(strList1);
            var list2        = ListNodeHelper.BuildList(strList2);
            var expectedList = ListNodeHelper.BuildList(expected);

            var sol = new Solution();
            var res = sol.MergeInBetween(list1, a, b, list2);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expectedList));
        }
예제 #16
0
        public void Test_Stack(string listStr, string expectedStr)
        {
            var head = ListNodeHelper.BuildList(listStr);

            var sol = new Solution();
            var res = sol.OddEvenList(head);

            var expected = ListNodeHelper.BuildList(expectedStr);

            Assert.IsTrue(ListNodeHelper.AreEqual(res, expected));
        }
예제 #17
0
        public void Test_Generic(string inputStr, string expectedStr)
        {
            var head     = ListNodeHelper.BuildList(inputStr);
            var expected = ListNodeHelper.BuildList(expectedStr);

            var sol = new Solution();

            sol.ReorderList(head);

            Assert.IsTrue(ListNodeHelper.AreEqual(head, expected));
        }
예제 #18
0
        public void AddTwoNumbersTest_2()
        {
            int[]    nums1 = { 6, 7, 8, 9, 9 };
            int[]    nums2 = { 1, 2, 3 };
            ListNode lst1  = ListNodeHelper.CreateLinkedListByArray(nums1);
            ListNode lst2  = ListNodeHelper.CreateLinkedListByArray(nums2);

            int[] expect = { 7, 9, 1, 0, 0, 1 };
            int[] res    = ListNodeHelper.GetElementsFromLinkedList(m02.AddTwoNumbers(lst1, lst2));

            Assert.IsTrue(CompareHelper.CompareArrays(expect, res));
        }
예제 #19
0
        public void Test_Generic <T>(Func <ISolution> constructor, int intersectVal, string listAStr, string listBStr, int skipA, int skipB)
            where T : ISolution
        {
            var listA = ListNodeHelper.BuildList(listAStr);
            var listB = ListNodeHelper.BuildList(listBStr);

            if (intersectVal != 0)
            {
                var a   = listA;
                var b   = listB;
                var skA = skipA;
                var skB = skipB;
                while (skA-- > 0)
                {
                    a = a.next;
                }
                while (skB-- > 1)
                {
                    b = b.next;
                }
                b.next = a;
            }

            var sol = constructor();
            var res = sol.GetIntersectionNode(listA, listB);

            if (intersectVal == 0)
            {
                Assert.AreEqual(null, res);
            }
            else
            {
                Assert.AreEqual(intersectVal, res?.val);
                while (skipA-- > 0)
                {
                    listA = listA.next;
                }
                while (skipB-- > 0)
                {
                    listB = listB.next;
                }
                Assert.AreEqual(res, listA);
                Assert.AreEqual(res, listB);
            }
        }
예제 #20
0
        public void Test_Generic(string[] operations, string inputs)
        {
            var head = ListNodeHelper.BuildList(inputs);
            var sol  = new Solution(head);

            Dictionary <int, int> res = new Dictionary <int, int>();
            int randoms = 0;

            foreach (var op in operations)
            {
                switch (op)
                {
                case "Solution":
                    break;

                case "getRandom":
                    int r = sol.GetRandom();
                    if (res.ContainsKey(r))
                    {
                        res[r]++;
                    }
                    else
                    {
                        res.Add(r, 1);
                    }
                    randoms++;
                    break;
                }
            }

            int avg = randoms / res.Count;

            foreach (var val in res.Values)
            {
                Assert.Greater(val, 0.7 * avg);
                Assert.Less(val, 1.3 * avg);
            }
        }
예제 #21
0
        public void Test_Generic(string list, int pos)
        {
            var head     = ListNodeHelper.BuildList(list);
            var expected = head;

            if (pos == -1)
            {
                expected = null;
            }
            else
            {
                for (int i = 0; i < pos; i++)
                {
                    expected = expected.next;
                }
                ListNodeHelper.Tail(head).next = expected;
            }

            var sol = new Solution();
            var res = sol.DetectCycle(head);

            Assert.AreEqual(res, expected);
        }