Beispiel #1
0
        public void run()
        {
            // 显示当前类名
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name);


            // 链表 a
            SingleListNode head1 = CommonDefinition.GenerateSingleList(new int[] { 1, 2, 3 });
            // 链表 b
            SingleListNode head2 = CommonDefinition.GenerateSingleList(new int[] { 4, 5, 6 });

            // 打印两个原始链表
            CommonDefinition.PrintSingleList(head1, "a: ");
            CommonDefinition.PrintSingleList(head2, "b: ");

            // 开玩
            // todo
            // return;
            SingleListNode tNode;

            tNode = head1;

            // 打印结果
            CommonDefinition.PrintSingleList(head1);
        }
        public void run()
        {
            // 显示当前类名
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name);

            SingleListNode tNode;

            // 链表 a
            SingleListNode head1 = CommonDefinition.GenerateSingleList(new int[] { 1, 2, 3 });
            // 链表 b
            SingleListNode head2 = CommonDefinition.GenerateSingleList(new int[] { 4, 5, 6 });

            // 打印两个原始链表
            CommonDefinition.PrintSingleList(head1, "a: ");
            CommonDefinition.PrintSingleList(head2, "b: ");

            // 开玩
            SingleListNode wrkNode     = head1;
            SingleListNode wrkNodeNext = head2;

            while (wrkNode != null)
            {
                tNode        = wrkNode.Next;
                wrkNode.Next = wrkNodeNext;
                wrkNodeNext  = tNode;
                wrkNode      = wrkNode.Next;
            }

            // 打印结果
            CommonDefinition.PrintSingleList(head1);
        }
        public void run()
        {
            // 显示当前类名
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name);

            // 链表 a
            SingleListNode head1 = CommonDefinition.GenerateSingleList(new int[] { 3, 3, 7, 9, 20 });
            // 链表 b
            SingleListNode head2 = CommonDefinition.GenerateSingleList(new int[] { 1, 3, 5, 6, 10 });

            // 打印两个原始链表
            CommonDefinition.PrintSingleList(head1, "a: ");
            CommonDefinition.PrintSingleList(head2, "b: ");

            // 开玩
            SingleListNode tNode;
            SingleListNode ret     = head1;
            SingleListNode h1      = head1;
            SingleListNode h1Prev  = null;
            SingleListNode h2      = head2;
            bool           isFirst = true;

            while (h1 != null)
            {
                while (h2 != null)
                {
                    if (h2.Val >= h1.Val)
                    {
                        break;
                    }

                    tNode   = h2.Next;
                    h2.Next = h1;
                    h1      = h2;
                    if (isFirst)
                    {
                        ret = h1;
                    }
                    else if (h1Prev != null)
                    {
                        h1Prev.Next = h1;
                    }

                    h2 = tNode;
                }

                isFirst = false;

                if (h1.Next == null && h1 != null)
                {
                    h1.Next = h2;
                }

                h1Prev = h1;
                h1     = h1.Next;
            }

            // 打印结果
            CommonDefinition.PrintSingleList(ret);
        }
Beispiel #4
0
        public void run()
        {
            // 显示当前类名
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name);

            // SingleListNode tNode;

            // 要被删掉的那个
            const int k = 4;

            // 默认给 5 个
            SingleListNode head = CommonDefinition.GenerateSingleList(5);

            // 打印原始链表
            CommonDefinition.PrintSingleList(head, "a: ");

            // 开玩
            SingleListNode tNode = head;
            int            idx   = 1; // 元素下标,1 开始

            while (idx < k)
            {
                if (tNode.Next == null)
                {
                    break;
                }

                if (idx == k - 1)
                {
                    SingleListNode nodeK = tNode.Next;
                    tNode.Next = nodeK.Next;
                    nodeK      = null;
                    break;
                }
                else
                {
                    tNode = tNode.Next;
                }

                idx++;
            }

            // 打印结果
            CommonDefinition.PrintSingleList(head, "--> ");
        }