C#反转单向链表

链表反转算法

 

    class Class1
ExpandedBlockStart.gifContractedBlock.gif    
{
        [STAThread]
        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//初始化链表
            LineNote startnote = new LineNote( "1" );
            LineNote note 
= startnote;
            
forint i = 2; i<= 5 ;i ++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                note.Next 
= new LineNote( i.ToString() );
                note 
= note.Next;
            }

            note 
= startnote;
            
//输出
            while ( note!=null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                System.Console.WriteLine( note.Note );
                note 
= note.Next;
            }

            
//开始反转
            note = startnote;
            LineNote temp 
= note;
            
while ( temp != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                note 
= startnote;
                
if ( temp != startnote )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    startnote 
= temp;
                    temp 
= temp.Next;
                    startnote.Next 
= note;
                }

                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    temp 
= temp.Next;
                    note.Next
= null;
                }

            }

            再次输出
            note 
= startnote;
            
while ( note!=null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                System.Console.WriteLine( note.Note );
                note 
= note.Next;
            }

        }

    }


    
class LineNote
ExpandedBlockStart.gifContractedBlock.gif    
{
        
public string Note;
        
public LineNote Next;
        
public LineNote( string note)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Note
=note;
        }

    }

 

 

        List reverse(List l)
        {
            if(!l)
                return l;
            list cur = l.next;
            list pre = l;
            list tmp;
            pre.next = null;
            while ( cur )
            {
                tmp = cur;
                cur = cur.next;
                tmp.next = pre;
                pre = tmp;
            }
            return tmp;
        }

转载于:https://www.cnblogs.com/zzp28/articles/1792221.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值