首页 > Windows开发 > 详细

C# 反转单向链表

时间:2019-08-10 00:25:47      阅读:225      评论:0      收藏:0      [点我收藏+]
 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;
    }

 

 

C# 反转单向链表

原文:https://www.cnblogs.com/dyfisgod/p/11330094.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!