Example #1
0
        public void Run()
        {
            /* Create linked list */
            int[] vals = {1, 3, 7, 5, 2, 9, 4};
            var head = new LinkedListNode(vals[0], null, null);
            var current = head;

            for (var i = 1; i < vals.Length; i++)
            {
                current = new LinkedListNode(vals[i], null, current);
            }
            Console.WriteLine(head.PrintForward());

            var head2 = head.Clone();
            var head3 = head.Clone();
            var head4 = head.Clone();

            /* Partition */
            var h = Partition(head, 5);
            var h2 = Partition2(head2, 5);
            var h3 = Partition3(head3, 5);
            var h4 = Partition4(head4, 5);

            /* Print Result */
            Console.WriteLine(h.PrintForward());
            Console.WriteLine(h2.PrintForward());
            Console.WriteLine(h3.PrintForward());
            Console.WriteLine(h4.PrintForward());
        }
        public void Run()
        {
            #region First Part

            {
                var lA1 = new LinkedListNode(9, null, null);
                var lA2 = new LinkedListNode(9, null, lA1);
                var lA3 = new LinkedListNode(9, null, lA2);

                var lB1 = new LinkedListNode(1, null, null);
                var lB2 = new LinkedListNode(0, null, lB1);
                var lB3 = new LinkedListNode(0, null, lB2);

                var list3 = AddLists(lA1, lB1, 0);

                Console.WriteLine("  " + lA1.PrintForward());
                Console.WriteLine("+ " + lB1.PrintForward());
                Console.WriteLine("= " + list3.PrintForward());

                var l1 = LinkedListToInt(lA1);
                var l2 = LinkedListToInt(lB1);
                var l3 = LinkedListToInt(list3);

                Console.Write(l1 + " + " + l2 + " = " + l3 + "\n");
                Console.WriteLine(l1 + " + " + l2 + " = " + (l1 + l2));
            }

            #endregion First Part

            #region Followup

            {
                var lA1 = new LinkedListNode(3, null, null);
                var lA2 = new LinkedListNode(1, null, lA1);
                //LinkedListNode lA3 = new LinkedListNode(5, null, lA2);

                var lB1 = new LinkedListNode(5, null, null);
                var lB2 = new LinkedListNode(9, null, lB1);
                var lB3 = new LinkedListNode(1, null, lB2);

                var list3 = AddLists2(lA1, lB1);

                Console.WriteLine("  " + lA1.PrintForward());
                Console.WriteLine("+ " + lB1.PrintForward());
                Console.WriteLine("= " + list3.PrintForward());

                var l1 = linkedListToInt(lA1);
                var l2 = linkedListToInt(lB1);
                var l3 = linkedListToInt(list3);

                Console.Write(l1 + " + " + l2 + " = " + l3 + "\n");
                Console.WriteLine(l1 + " + " + l2 + " = " + (l1 + l2));
            }

            #endregion Followup
        }