第503篇--Reverse one direction linked nodes

This post is just used to review the link related knowledges. There are two methods we can use to reverse a linked nodes, one is recursive, another is non-recursive.  Both are contained in this post, since the code is quite simple, I am not going to illustrate this in details, you can just practice it yourself.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterviewDemo
{
    class Class6
    {
        [STAThread]
        static void Main(string[] args)
        {
            //Initialize the link
            LineNote startnote = new LineNote("1");
            LineNote note = startnote;
            for (int i = 2; i <= 5; i++)
            {
                note.Next = new LineNote(i.ToString());
                note = note.Next;
            }
            note = startnote;

            note = ReverseLink(startnote);
            while (note != null)
            {
                System.Console.Write(note.Note + "-->");
                note = note.Next;
            }
            
        }

        /// <summary>
        /// Recursive algorithm
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private static LineNote ReverseLink(LineNote list)
        {
            if (list.Next == null)
            {
                return list;
            }
            else
            {
                LineNote n = ReverseLink(list.Next);
                list.Next.Next = list;
                list.Next = null;
                return n;
            }
        }

        /// <summary>
        /// 1 Non-recursive
        /// </summary>
        /// <param name="startnote"></param>
        private static  void Reverse1(LineNote startnote)
        {
            // 1 开始反转
            LineNote note = startnote;
            LineNote temp = note;
            while (temp != null)
            {
                note = startnote;
                if (temp != startnote)
                {
                    startnote = temp;
                    temp = temp.Next;
                    startnote.Next = note;
                }
                else
                {
                    temp = temp.Next;
                    note.Next = null;
                }
            }

            note = startnote;
            while (note != null)
            {
                System.Console.Write(note.Note + "-->");
                note = note.Next;
            }
        }

        /// <summary>
        /// 2 Non-recursive
        /// </summary>
        /// <param name="startnote"></param>
        private static void Reverse2(LineNote startnote)
        {
            // 2 反转
            // r,q,p 一步一步,像走路一样
            // p总是指向下一个节点
            //q 用来跟上p
            //r 用来存储断开的节点
            LineNote r = null;
            LineNote q = null;
            LineNote p = startnote;

            while (p != null)
            {
                r = q;
                q = p;
                p = p.Next;
                q.Next = r;
            }

            LineNote note = q;
            while (note != null)
            {
                System.Console.Write(note.Note + "-->");
                note = note.Next;
            }
        }
    }

    class LineNote
    {
        public string Note;
        public LineNote Next;
        public LineNote(string note)
        {
            Note = note;
        }
    }
}

 

posted @ 2013-02-24 21:13  Shanghai Jim Zhou  阅读(214)  评论(0编辑  收藏  举报