C# 反转单向链表

 static void Main(string[] args)
        {

            Mynode mynode0 = new Mynode();
            Mynode mynode1 = new Mynode();
            Mynode mynode2 = new Mynode();
            Mynode mynode3 = new Mynode();
            Mynode mynode4 = new Mynode();
            Mynode mynode5 = new Mynode();

            mynode0.Value = 0;
            mynode0.Next = mynode1;
            mynode1.Value = 1;
            mynode1.Next = mynode2;
            mynode2.Value = 2;
            mynode2.Next = mynode3;
            mynode3.Value = 3;
            mynode3.Next = mynode4;
            mynode4.Value = 4;
            mynode4.Next = mynode5;
            mynode5.Value = 5;
            mynode5.Next = null;

            mynode0 = Reverse(mynode0, mynode0.Next);
        }

static Mynode Reverse(Mynode current, Mynode next)
        {
            Mynode tmp = next.Next;
            next.Next = current;
            if (current.Value == 0)//判断头
            {
                current.Next = null;
            }
            if (tmp == null)
            {
                return next;
            }
            else
            {
                return Reverse(next, tmp);
            }

        }

 public class Mynode
    {
        public int Value;
        public Mynode Next;
    }

 

 

posted @ 2019-08-09 23:32  dyfisgod  阅读(334)  评论(0编辑  收藏  举报