Exemple #1
0
        public void TestMethod1(int[] head, int[] expected)
        {
            // Arrange
            SwapNodesinPairs question = new SwapNodesinPairs();
            ListNode         headNode = null;

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

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

            // Act
            ListNode   actual     = question.SwapPairs(headNode);
            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());
        }
        public void SwapPairsTest()
        {
            //Arrange
            var swapNodesinPairs = new SwapNodesinPairs();
            var actInput         = new ListNode(1)
            {
                next = new ListNode(2)
                {
                    next = new ListNode(3)
                    {
                        next = new ListNode(4)
                    }
                }
            };
            var expected = new ListNode(2)
            {
                next = new ListNode(1)
                {
                    next = new ListNode(4)
                    {
                        next = new ListNode(3)
                    }
                }
            };
            //Act
            var act = swapNodesinPairs.SwapPairs(actInput);

            //Assert
            Assert.AreEqual(expected, act);
        }
Exemple #3
0
        public void SwapPairsTests()
        {
            SwapNodesinPairs obj = new SwapNodesinPairs();


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

            var x = obj.SwapPairs(current);// 2 1 4 3

            current = new ListNode(1)
            {
            };

            x = obj.SwapPairs(current);

            current = new ListNode(1)
            {
                next = new ListNode(2)
                {
                    next = new ListNode(3)
                    {
                    }
                }
            };

            x = obj.SwapPairs(current);

            current = null;
            x       = obj.SwapPairs(current);
        }