コード例 #1
0
        public void TestSimple()
        {
            Assert.IsTrue(ListNode.Equals(ListNode.Create(new int[] { 1, 2, 3, 4, 5 }),
                                          util.MergeTwoLists(ListNode.Create(new int[] { 1, 2, 3 }), ListNode.Create(new int[] { 4, 5 }))));

            Assert.IsTrue(ListNode.Equals(ListNode.Create(new int[] { 1 }), util.MergeTwoLists(null, ListNode.Create(new int[] { 1 }))));
        }
コード例 #2
0
        public void MergeTwoListsTests()
        {
            MergeTwoSortedLists obj = new MergeTwoSortedLists();

            ListNode l1 = new ListNode(1)
            {
                next = new ListNode(2)
                {
                    next = new ListNode(4)
                    {
                    }
                }
            };

            ListNode l2 = new ListNode(1)
            {
                next = new ListNode(3)
                {
                    next = new ListNode(4)
                    {
                    }
                }
            };

            var x = obj.MergeTwoLists(l1, l2);


            l1 = null;

            l2 = new ListNode(1)
            {
                next = new ListNode(3)
                {
                    next = new ListNode(4)
                    {
                    }
                }
            };

            x = obj.MergeTwoLists(l1, l2);


            l1 = new ListNode(1)
            {
                next = new ListNode(2)
                {
                    next = new ListNode(4)
                    {
                    }
                }
            };

            l2 = null;

            x = obj.MergeTwoLists(l1, l2);
        }
コード例 #3
0
        public void MergeTwoLists()
        {
            var a5 = new ListNode(5);
            var a6 = new ListNode(6);
            var a7 = new ListNode(7);

            a5.Next = a6;
            a6.Next = a7;
            var l1 = a5;

            var b1  = new ListNode(1);
            var b9  = new ListNode(9);
            var b10 = new ListNode(10);
            var b20 = new ListNode(20);

            b1.Next  = b9;
            b9.Next  = b10;
            b10.Next = b20;
            var l2 = b1;

            var actual = _sut.MergeTwoLists(l1, l2);

            var expected = b1;

            Assert.AreEqual(expected, actual);
        }
コード例 #4
0
        public void MergeTwoListsTest(int[] list1, int[] list2, int[] expected)
        {
            var target = new MergeTwoSortedLists();

            var actual = target.MergeTwoLists(
                target.CreateListNode(list1),
                target.CreateListNode(list2));

            actual.Should().BeEquivalentTo(target.CreateListNode(expected));
        }
コード例 #5
0
        public void MergeTwoLists_WhenGivenFirstLeetCodeExample_ReturnsCorrectOutput()
        {
            // Arrange
            ListNode l1 = new ListNode(1);

            l1.next      = new ListNode(2);
            l1.next.next = new ListNode(4);

            ListNode l2 = new ListNode(1);

            l2.next      = new ListNode(3);
            l2.next.next = new ListNode(4);

            // Act
            var result = _mergeTwoSortedLists.MergeTwoLists(l1, l2);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.val);
        }
コード例 #6
0
        public void MergeTwoSortedListsTest()
        {
            ListNode l1 = new ListNode(1, new ListNode(2, new ListNode(4, null)));
            ListNode l2 = new ListNode(1, new ListNode(3, new ListNode(4, null)));

            var mergeTwoSortedLists = new MergeTwoSortedLists();

            var      actual   = mergeTwoSortedLists.MergeTwoLists(l1, l2);
            ListNode expected = new ListNode(1, new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(4, null))))));

            Assert.That(actual, Is.Not.Null);
        }
コード例 #7
0
        public void TestMethod1(int[] headL1, int[] headL2, int[] expected)
        {
            // Arrange
            MergeTwoSortedLists question = new MergeTwoSortedLists();
            ListNode            l1       = null;

            if (headL1 != null && headL1.Length > 0)
            {
                l1 = new ListNode(headL1[0]);
                ListNode node = l1;

                for (int i = 1; i < headL1.Length; i++)
                {
                    node.next = new ListNode(headL1[i]);
                    node      = node.next;
                }
            }
            ListNode l2 = null;

            if (headL2 != null && headL2.Length > 0)
            {
                l2 = new ListNode(headL2[0]);
                ListNode node = l2;

                for (int i = 1; i < headL2.Length; i++)
                {
                    node.next = new ListNode(headL2[i]);
                    node      = node.next;
                }
            }

            // Act
            ListNode   actual     = question.MergeTwoLists(l1, l2);
            List <int> actualList = null;

            if (actual != null)
            {
                actualList = new List <int>();
                while (actual != null)
                {
                    actualList.Add(actual.val);
                    actual = actual.next;
                }
            }

            // Assert
            CollectionAssert.AreEqual(expected, actualList?.ToArray());
        }
コード例 #8
0
        public void Given_two_single_node_list_When_merged_Then_two_node()
        {
            ListNode node1 = new ListNode(1);
            ListNode node2 = new ListNode(2);

            var node = MergeTwoSortedLists.MergeTwoLists(node1, node2);

            List <int> result = new List <int>()
            {
                1, 2
            };
            var index = 0;

            while (node != null)
            {
                Assert.IsTrue(index < result.Count);
                Assert.AreEqual(result[index], node.val);
                node = node.next;
                index++;
            }
        }
コード例 #9
0
        public void Test_MergeTwoLists()
        {
            var l1 = new ListNode(1);

            l1.next      = new ListNode(2);
            l1.next.next = new ListNode(4);

            var l2 = new ListNode(1);

            l2.next      = new ListNode(3);
            l2.next.next = new ListNode(4);

            var expected = new int[] { 1, 1, 2, 3, 4, 4 };
            var actual   = _objUnderTest.MergeTwoLists(l1, l2);

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.Equal(expected[i], actual.val);
                actual = actual.next;
            }
        }
コード例 #10
0
        void InternalTest(ListNode l1, ListNode l2, ListNode expected)
        {
            ListNode actual = MergeTwoSortedLists.MergeTwoLists(l1, l2);

            AssertHelper.AssertListNode(expected, actual);
        }