C# 反转单向链表

你说的曾经没有我的故事 提交于 2019-11-26 18:08:20
 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;
    }

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!