public int[] TestSolution(int[] l1, int[] l2) { ListNode node1 = new ListNode(l1); ListNode node2 = new ListNode(l2); return(MergeTwoSortedLists.Solution(node1, node2).ToArray()); }
public void MergeTwoLists_BothEmpty_ShouldMerge() { var result = MergeTwoSortedLists.Merge( new List <int>(), new List <int>()); result.Should().NotBeNull(); }
public void MergeTwoLists_Nulls_ShouldThrow() { Action action = () => { MergeTwoSortedLists.Merge(null, null); }; action.Should().Throw <ArgumentNullException>(); }
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); }
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)); }
public void MergeTwoLists_SecondNull_ShouldThrow() { Action action = () => { MergeTwoSortedLists.Merge( null, new List <int>()); }; action.Should().Throw <ArgumentNullException>(); }
public void MergeTwoSortedListsTests() { var l1 = new ListNode(1, new ListNode(2, new ListNode(4))); var l2 = new ListNode(1, new ListNode(3, new ListNode(4))); var result = new ListNode(1, new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(4)))))); var solver = new MergeTwoSortedLists(); var res = solver.Solution(l1, l2); Assert.AreEqual(res, result); }
public void MergeTwoLists_SecondEmpty_ShouldMerge() { var result = MergeTwoSortedLists.Merge( new List <int>() { 1 }, new List <int>()); result.Should().HaveCount(1); result[0].Should().Be(1); }
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); }
public void MergeTwoSortedArraysTest() { int[] arr1 = { -2120996231, -1712935037, -1703673711, -1646095928, -1626097495, -1615680386, -1597131699, -1570326789, -1523090300, -1505782987, -1475588920, -1413239376, -1406129179, -1400568380, -1268858441, -1249391611, -1246083490, -1146477631, -1083291644, -1073939103, -1043077206, -1041739357, -1036731760, -1033272347, -970190212, -937576457, -865905850, -813031026, -771745552, -765507965, -733255990, -706572903, -667937720, -628689890, -597415519, -582800000, -571329269, -535481901, -435616485, -411950692, -325996333, -303740798, -193260330, -191136906, -172604141, -145592722, -51880142, -25937243, -18881257, -9780, -9540, -9490, -8980, -8856, -8753, -8455, -8390,-8328, -7929, -7772, -7423, -7412, -7224, -7208, 46412230, 46920758 }; int[] arr2 = { -2120996231, -2039897248, -2004579200, -1976555129, -1974066388, -1881537032, -1807815184, -1730567509, -6992, -6827, -6731, -6138, -5751, -5676, -5483, -5354, -5217, -5177, -5036, -4710, -4697, -4694, -4641, -4483, -4101, -3969, -3733, -3690, -3565, -3399, -3243, -3041, -3015, -2866, -2776, -2706, -2602, -2569, -2400, -1519, -533, -422, -420, -347, -205, 430, 540, 1273, 1349, 1730, 1812, 1908, 2103, 2135, 2181, 2729, 3165, 3394, 3704, 3868, 3924, 4112, 4147, 4331, 4409, 4589, 4916, 5136, 5385, 5735, 5843, 6050, 6245, 6304, 6695, 7088, 7442, 7510, 7631, 7880, 7970, 8118, 8361, 8488, 8586, 8780, 8900, 8948, 9177, 9215, 9449, 9700, 9734 }; var merge = new MergeTwoSortedLists(); int[] result = merge.Merge(arr1, arr2); int[] expected = { -2120996231, -2120996231, -2039897248, -2004579200, -1976555129, -1974066388, -1881537032, -1807815184, -1730567509, -1712935037, -1703673711, -1646095928, -1626097495, -1615680386, -1597131699, -1570326789, -1523090300, -1505782987, -1475588920, -1413239376, -1406129179, -1400568380, -1268858441, -1249391611, -1246083490, -1146477631, -1083291644, -1073939103, -1043077206, -1041739357, -1036731760, -1033272347, -970190212, -937576457, -865905850, -813031026, -771745552, -765507965, -733255990, -706572903, -667937720, -628689890, -597415519, -582800000, -571329269, -535481901, -435616485, -411950692, -325996333, -303740798, -193260330, -191136906, -172604141, -145592722, -51880142, -25937243,-18881257, -9780, -9540, -9490, -8980, -8856, -8753, -8455, -8390, -8328, -7929, -7772, -7423, -7412, -7224, -7208, -6992, -6827, -6731, -6138, -5751, -5676, -5483, -5354, -5217, -5177, -5036, -4710, -4697, -4694, -4641, -4483, -4101, -3969, -3733, -3690, -3565, -3399, -3243, -3041, -3015, -2866, -2776, -2706, -2602, -2569, -2400, -1519, -533, -422, -420, -347, -205, 430, 540, 1273, 1349, 1730, 1812, 1908, 2103, 2135, 2181, 2729, 3165, 3394, 3704, 3868, 3924, 4112, 4147, 4331, 4409, 4589, 4916, 5136, 5385, 5735, 5843, 6050, 6245, 6304, 6695, 7088, 7442, 7510, 7631, 7880, 7970, 8118, 8361, 8488, 8586, 8780, 8900, 8948, 9177, 9215, 9449, 9700, 9734, 46412230, 46920758 }; Assert.That(result, Is.EqualTo(expected)); }
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()); }
public void MergeTwoLists_BothWithSameElement_ShouldMerge() { var result = MergeTwoSortedLists.Merge( new List <int>() { 1 }, new List <int>() { 1 }); result.Should().HaveCount(2); result[0].Should().Be(1); result[1].Should().Be(1); }
public void MergeTwoLists_BothWithOneDistinctUnsortedElement_ShouldMergeSorted() { var result = MergeTwoSortedLists.Merge( new List <int>() { 2 }, new List <int>() { 1 }); result.Should().HaveCount(2); result[0].Should().Be(1); result[1].Should().Be(2); }
public void MergeTwoLists_SecondAreLast_ShouldMergeSorted() { var result = MergeTwoSortedLists.Merge( new List <int>() { 1, 2 }, new List <int>() { 10 }); result.Should().HaveCount(3); result[0].Should().Be(1); result[1].Should().Be(2); result[2].Should().Be(10); }
public void MergeTwoLists_DisctinctSizesSecondBigger_ShouldMergeSorted() { var result = MergeTwoSortedLists.Merge( new List <int>() { 1 }, new List <int>() { 1, 2 }); result.Should().HaveCount(3); result[0].Should().Be(1); result[1].Should().Be(1); result[2].Should().Be(2); }
public void MergeTwoSortedListsResultsInSuccess() { 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); var expectedResult = new ListNode(1); var prev = expectedResult; var node = new ListNode(1); prev.next = node; prev = node; node = new ListNode(2); prev.next = node; prev = node; node = new ListNode(3); prev.next = node; prev = node; node = new ListNode(4); prev.next = node; prev = node; node = new ListNode(4); prev.next = node; prev = node; var result = new MergeTwoSortedLists().MergeTwoLists(L1, L2); Assert.IsTrue(EnumerateLinkedLists(result.next, expectedResult)); }
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++; } }
public void MergeTwoLists_RandomCase_ShouldMergeSorted() { var result = MergeTwoSortedLists.Merge( new List <int>() { -71, 2, 12345 }, new List <int>() { -100, 1, 2, 12344 }); result.Should().HaveCount(7); result[0].Should().Be(-100); result[1].Should().Be(-71); result[2].Should().Be(1); result[3].Should().Be(2); result[4].Should().Be(2); result[5].Should().Be(12344); result[6].Should().Be(12345); }
static void Main(string[] args) { SortedMatrixSearch.Run(); SparseSearch.Run(); SearchInRotatedArray.Run(); GroupAnagrams.Run(); CombinationsOfNPairsParentheses.Run(); PermutationWithDuplicates.Run(); PermutationNoDuplicates.Run(); var subsetList = new List <List <int> >(); subsetList = SubsetInSet.FindAllSubsetInSet(new List <int> { 1, 2, 3 }); ReverseLinkedList.Run(); IsUniqueString.Run(); StoneDivisionProblem.Run(); Kangaroo.Run(); AppleAndOrange.Run(); AbbreviationProblem.Run(); FibonacciModifiedProblem.Run(); RecursiveDigitSum.Run(); RangeSumOfBST.Run(); GradingStudentsProblem.Run(); // XorSequenceProblem.Run(); CounterGameProblem.Run(); MaximizingXORProblem.Run(); LonelyIntegerProblem.Run(); FlippingBitsProblem.Run(); QueueUsingTwoStacksProblem.Run(); GetNodeValue.Run(); MergeTwoSortedLinkedLists.Run(); Compare_Two_linked_lists.Run(); DeleteNodeProblem.Run(); ArrayManipulationProblem.Run(); LeftRotationProblem.Run(); HourGlass2D.Run(); SimpleTextEditorProblem.Run(); EqualStacksProblem.Run(); MaximumElementProblem.Run(); BinarySearchTreeInsertion.Run(); TopViewProblem.Run(); TimeConvertsionProblem.Run(); BinaryTreePathsProblem.Run(); IncreasingOrderSearchTree.Run(); RemoveAllAdjacentDuplicatesInStringWithKLength.Run(); RemoveAllAdjacentDuplicatesInString.Run(); CheckStraightLineProblem.Run(); HouseRobber.Run(); UniquePathsProblem.Run(); FirstUniqueCharacterInString.Run(); BinaryTreeInorderTraversal.Run(); DailyTemperaturesProblem.Run(); CountingBitsproblem.Run(); SortIntegersByTheNumberOf1BitsProblem.Run(); HammingDistanceProblem.Run(); RansomNoteProblem.Run(); ConvertBinaryNumberInLinkedListToIntegerProblem.Run(); NumberOfStepsToReduceNumberToZeroProblem.Run(); JewelsAndStones.Run(); ClimbingStairsProblem.Run(); BestTimeToBuyAndSellStock.Run(); MajorityElementProblem.Run(); MoveZeroesProblem.Run(); InvertBinaryTree.Run(); SingleNumberProblem.Run(); MaximumDepthInTrree.Run(); MergeTwoBinaryTrees.Run(); AddBinaryProblem.Run(); PlusOneProblem.Run(); LengthOfLastWordProblem.Run(); KadaneAlgorithmForMaxSubArray.Run(); KMPAlgorithm.Run(); CountAndSayProblem.Run(); SearchInsertPosition.Run(); ImplementIndexOfString.Run(); RemoveElement.Run(); RemoveDuplicatesFromSortedArray.Run(); MergeTwoSortedLists.Run(); ValidParentheses.Run(); LongestCommonPrefix.Run(); RomanToInteger.Run(); PalindromeNumber.Run(); ReverseInteger.Run(); TwoSumProblem.Run(); AddOneToNumber.Run(); MostAmountOfChange.Run(); #region BinaryTree LeastCommonAncestor.Run(); PrintAllPaths.Run(); HasPathSum.Run(); CheckIfBinaryTreeIsBinarySearchTree.Run(); PrintAllNodesWithRangeInBinarySearchTree.Run(); UniqueTreeStructureNumber.Run(); MirrorTree.Run(); #region BitManuiplation_GetNthNumber NumberOfStepsToReduceNumberToZeroProblem.Run(); CountNumbersOf1InBit.Run(); ReverseThebitsInInteger.Run(); PrintBitsInInteger.Run(); GetNthBit.Run(); setNthBitTo1.Run(); SetNthBitTo0.Run(); #endregion MinimumtValueInTrree minValueInTree = new MinimumtValueInTrree(); minValueInTree.Run(); #endregion #region Recursion Chessboard chessboard = new Chessboard(); chessboard.Run(); RatPathToMaze ratPathToMaze = new RatPathToMaze(); ratPathToMaze.Run(); List <string> anagramList = new List <string>(); anagramList = WordAnagram.GenerateWordAnagram("abc"); Pixel[,] pixelList = new Pixel[3, 3] { { new Pixel(0, 0, "red"), new Pixel(0, 1, "green"), new Pixel(0, 2, "green") }, { new Pixel(1, 0, "red"), new Pixel(1, 1, "green"), new Pixel(1, 2, "green") }, { new Pixel(2, 0, "red"), new Pixel(2, 1, "green"), new Pixel(2, 2, "green") } }; FillPaint.PaintFill(pixelList, 1, 2, "green", "black"); BinaryTreesAreTheSame.Run(); #endregion #region General problems RotateArrayByKSpaces.Run(); #region AddtwoNumbersReferencedByTheirDigits var addRes = AddtwoNumbersReferencedByTheirDigits.AddNumbers(new int[] { 1, 2, 7 }, new int[] { 9, 4 }); #endregion #region RunLengthEncoding var encodedStr = RunLengthEncoding.Encode("aabbbbc"); var decodedStr = RunLengthEncoding.Decode(encodedStr); #endregion #region BreakDocumentIntoChunk var chunkRes = BreakDocumentIntoChunk.Chunkify("ab:dd:ddfcct:aab:cccc", ':', 5); #endregion #region GameOfLife var gameRes = GameOfLife.GetNextInteration(new int[3, 3] { { 1, 0, 0 }, { 0, 1, 1 }, { 1, 0, 0 } }); #endregion . #endregion #region InsertionSort InsertionSort.insertionSort(listToSort); #endregion #region BinarySearch Console.WriteLine(String.Format("%s is present at index: %s", 30, BinarySearch.binarySearch(sortedArray, 30, 0, sortedArray.Length - 1))); Console.WriteLine(String.Format("%s is present at index: %s", 4, BinarySearch.binarySearch(sortedArray, 4, 0, sortedArray.Length - 1))); Console.WriteLine(String.Format("%s is present at index: %s", 15, BinarySearch.binarySearch(sortedArray, 15, 0, sortedArray.Length - 1))); #endregion #region MergeSort MergeSort.print(listToSort); MergeSort.mergeSort(listToSort); #endregion #region QuickSort QuickSort.print(listToSort); QuickSort.quickSort(listToSort, 0, listToSort.Length - 1); QuickSort.print(listToSort); #endregion }
void InternalTest(ListNode l1, ListNode l2, ListNode expected) { ListNode actual = MergeTwoSortedLists.MergeTwoLists(l1, l2); AssertHelper.AssertListNode(expected, actual); }
public MergeTwoSortedListsTests() { _mergeTwoSortedLists = new MergeTwoSortedLists(); }
public MergeTwoSortedListsTests() { _objUnderTest = new MergeTwoSortedLists(); }
public void Setup() { _sut = new MergeTwoSortedLists(); }