/// <summary> /// Probs the 2 5 sum list test. /// You have two numbers represented by a linked list. Where each node /// contains a single digit. The digits are stored in REVERSE order, such /// that 1's digit is at the head of the list. Write a function that adds /// the two numbers and returns the sum as a linked list. /// /// Example: 1 /// Input: (7->1->6) + (5->9->2) Which is 617 + 295 /// Output 2->1->9 that is 912 /// </summary> public static void Prob_2_5_SumList_ReverOrder_Test() { var input1 = new int[] { 7, 1, 6 }; var input2 = new int[] { 5, 9, 2 }; var number1 = LinkedNode <int> .CreateNodesFromList2(input1); var number2 = LinkedNode <int> .CreateNodesFromList2(input2); var expectedResult = new int[] { 2, 1, 9 }; Console.WriteLine($"Input: 1"); number1.PrintValues(); Console.WriteLine($"Input: 2"); number2.PrintValues(); Console.WriteLine($"Expected output {expectedResult.ToString()}"); var t = LinkedNode <int> .CreateNodesFromList2(expectedResult); t.PrintValues(); var output = AddTwoReservedNumsAsLists(number1, number2); Console.WriteLine($"Actual output:"); output.PrintValues(); }
/// <summary> /// Probs the 2 4 partition test. /// Write code to partition a linked list around a value X, such that all /// nodes less than X come before all nodes greater than or equal to X. /// If X is contained within the list, the values of X only need to be /// after the elements than X . The partition element X can appear anywhere /// in the "right partition"; it does not need to appear between the left /// and right partitions. (so X can also be before values greater than X /// as long as those greater values come after the values less than X, see the /// example below, where 10 comes before 5, but after all values below 5). /// /// Example: Partition is 5 /// Input: 3->5->8->5->10->2->1 /// Output: 3->1->2->10->5->5->8 /// </summary> public static void Prob_2_4_Partition_Test() { //var inputArray = new int[] { 3, 5, 8, 5, 10, 2, 1 }; var inputArray = new int[] { 5, 10, 3, 5, 8, 5, 10, 2, 1, 5 }; var input = LinkedNode <int> .CreateNodesFromList2(inputArray); var partition = 5; Console.WriteLine($"Partition: {partition}. Input: "); input.PrintValues(); var output = Prob_2_4_Partition_v1(input, partition); Console.WriteLine($"All numbers small than {partition} should be first."); Console.WriteLine($"Actual output:"); output.PrintValues(); }