链表的倒置

    技术2022-05-12  0

            public static void ReOrder(ref Node listHead)        {            if (listHead == null)            {                throw new ArgumentNullException("list Head is null!");            }

                Node leftHead = listHead;            Node rightHead = null;            Node current = leftHead.Next;            leftHead.Next = null;

                while (current != null)            {                rightHead = current.Next;                current.Next = leftHead;                leftHead = current;                current = rightHead;            }

                listHead = leftHead;        }

     

    下面是程序详细代码:

     

       class Node    {        private int _value = 0;        public int NodeValue        {            get { return this._value; }            set { this._value = value; }        }

            private Node _next = null;        public Node Next        {            get { return this._next; }            set { this._next = value; }        }

            public Node()        {            this._value = 0;        }

            public Node(int nodeValue)        {            this._value = nodeValue;        }    }

     

        class ListUtil    {        public static void ReOrder(ref Node listHead)        {            listHead = ReOrder2(listHead);        }

            public static void ReOrder(Node listHead)        {            listHead = ReOrder2(listHead);        }

            private static Node ReOrder2(Node listHead)        {            if (listHead == null)            {                throw new ArgumentNullException("list Head is null!");            }

                Node leftHead = listHead;            Node rightHead = null;            Node current = leftHead.Next;            leftHead.Next = null;

                while (current != null)            {                rightHead = current.Next;                current.Next = leftHead;                leftHead = current;                current = rightHead;            }

                return leftHead;        }

            public static void ShowListInfo(Node listHead)        {            if (listHead == null)            {                throw new ArgumentNullException("listHead");            }

                Console.WriteLine("{0,-7}{1}","Index","Value");            int i = 0;            while (listHead != null)            {                i++;                Console.WriteLine("{0,-7}{1}",i,listHead.NodeValue);                listHead = listHead.Next;            }        }    }

     

        class Program    {        static void Main(string[] args)        {            Node a = new Node(1);            Node b = new Node(2);            Node c = new Node(3);            Node d = new Node(4);            Node e = new Node(5);            Node f = new Node(6);            a.Next = b;            b.Next = c;            c.Next = d;            d.Next = e;            e.Next = f;            f.Next = null;

                Console.WriteLine("Before Reorder:");            ListUtil.ShowListInfo(a);

               //ListUtil.ReOrder(ref a);           //Console.WriteLine("After Reorder *With* ref:");            ListUtil.ReOrder(a);            Console.WriteLine("After Reorder *Without* ref:");            ListUtil.ShowListInfo(a);

                Console.Read();        }    }

     

     


    最新回复(0)