Beispiel #1
0
        public void AddTwoNumbersTest()
        {
            var      s = new Solution();
            ListNode l1, l2, expected;

            l1       = ListNode.Build(2, 4, 3);
            l2       = ListNode.Build(5, 6, 4);
            expected = ListNode.Build(7, 0, 8);
            Assert.Equal(expected, s.AddTwoNumbers(l1, l2));

            l1       = ListNode.Build(0);
            l2       = ListNode.Build(0);
            expected = ListNode.Build(0);
            Assert.Equal(expected, s.AddTwoNumbers(l1, l2));

            l1       = ListNode.Build(0);
            l2       = ListNode.Build(1);
            expected = ListNode.Build(1);
            Assert.Equal(expected, s.AddTwoNumbers(l1, l2));

            l1       = ListNode.Build(5);
            l2       = ListNode.Build(5);
            expected = ListNode.Build(0, 1);
            Assert.Equal(expected, s.AddTwoNumbers(l1, l2));

            l1       = ListNode.Build(9);
            l2       = ListNode.Build(1, 9, 9, 9, 9, 9, 9, 9, 9, 9);
            expected = ListNode.Build(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
            Assert.Equal(expected, s.AddTwoNumbers(l1, l2));

            l1       = ListNode.Build(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
            l2       = ListNode.Build(5, 6, 4);
            expected = ListNode.Build(6, 6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
            Assert.Equal(expected, s.AddTwoNumbers(l1, l2));
        }
Beispiel #2
0
        public void OddEvenListTest()
        {
            var      s = new Solution();
            ListNode input, expected;

            int[] array;

            input    = ListNode.Build(1, 2, 3, 4, 5);
            expected = ListNode.Build(1, 3, 5, 2, 4);
            Assert.Equal(expected, s.OddEvenList(input));

            input    = ListNode.Build(2, 1, 3, 5, 6, 4, 7);
            expected = ListNode.Build(2, 3, 6, 7, 1, 5, 4);
            Assert.Equal(expected, s.OddEvenList(input));

            input    = ListNode.Build(1, 2, 3, 4);
            expected = ListNode.Build(1, 3, 2, 4);
            Assert.Equal(expected, s.OddEvenList(input));

            input    = ListNode.Build(1, 2, 3, 4, 5, 6);
            expected = ListNode.Build(1, 3, 5, 2, 4, 6);
            Assert.Equal(expected, s.OddEvenList(input));

            // Test Case 70/71
            array = ReadIntArrayFromFile("OddEvenList_input.txt");
            input = ListNode.Build(array);

            array    = ReadIntArrayFromFile("OddEvenList_output.txt");
            expected = ListNode.Build(array);
            Assert.Equal(expected, s.OddEvenList(input));
        }
Beispiel #3
0
        public void MiddleNodeTest()
        {
            var      s = new Solution();
            ListNode node;

            node = ListNode.Build(1, 2, 3, 4, 5);
            Assert.Equal(3, s.MiddleNode(node).val);

            node = ListNode.Build(1, 2, 3, 4, 5, 6);
            Assert.Equal(4, s.MiddleNode(node).val);
        }
Beispiel #4
0
        public void RemoveElementsTest()
        {
            var      s = new Solution();
            ListNode head, expected;
            int      val;

            head     = ListNode.Build(1, 2, 6, 3, 4, 5, 6);
            val      = 6;
            expected = ListNode.Build(1, 2, 3, 4, 5);
            Assert.Equal(expected, s.RemoveElements(head, val));
        }
Beispiel #5
0
        public void DeleteNodeTest()
        {
            var      s = new Solution();
            ListNode before, after, nodeToDelete;

            before = ListNode.Build(1, 2, 3, 4, 5);
            after  = ListNode.Build(1, 2, 4, 5);

            nodeToDelete = before;
            while (nodeToDelete.val != 3)
            {
                nodeToDelete = nodeToDelete.next;
            }

            s.DeleteNode(nodeToDelete);
            Assert.Equal(before, after);
        }