如何将链表倒序排列 c#程序员面试笔记

 public class LNode<T>   //一次遍历原地反转 
    {
        public T data;
        public LNode<T> next;
        //逆序算法01
        public static void  Reverse01(LNode<T> head)
        {
            //链表反转  保证至少有两个元素
            //head==null 未初始化的链表
            //head.next==null  仅包含头节点
            //head.next.next==null   一个元素
            if (head == null || head.next == null||head.next.next==null)
            {
                return;
            }            
            LNode<T> pre;
            LNode<T> cur;
            LNode<T> next;
            cur = head.next;
            next = cur.next;
            cur.next = null;  //表首节点变为表尾节点
            if (next.next == null) //只有两个元素的情况
            {
                head.next = next;
                next.next = cur;
                return;
            }
            while (next.next != null) //三个元素以上
            {
                pre = next.next;
                next.next = cur;
                cur = next;
                next = pre;
            }
            head.next = next;
            next.next = cur;

        }


        //递归逆序算法02
        private static LNode<T> ReverseWithoutHead(LNode<T> firstNode)
        {
            //firstnode是第一个非head节点
            if (firstNode == null || firstNode.next == null)
            {
                return firstNode;
            }
            else
            {
                LNode<T> newHead= ReverseWithoutHead(firstNode.next);            
                firstNode.next.next= firstNode;
                firstNode.next = null;
                return newHead;
            }
        }

        public static void Reverse02(LNode<T> head)
        {
            if (head == null)
            {
                return;
            }
            LNode<T> newnode = ReverseWithoutHead(head.next);
            head.next = newnode;
        }

        //插入算法03
        public static  void Reserve03(LNode<T> head)
        {
            //head不存储数据,保证有两个表项
            if (head == null || head.next == null || head.next.next == null)
            {
                return;
            }
            else{
                LNode<T> cur;
                LNode<T> next;

                cur = head.next.next;   
                head.next.next = null;  //曾经的头节点指向null
                while (cur != null)
                {
                    next = cur.next;
                    cur.next = head.next;
                    head.next = cur;
                    cur = next;
                }


            }

        }

    }

测试:

static void Main(string[] args)
        {
            LNode<int> l05 = new LNode<int>() { data =5, next = null };
            LNode<int> l04 = new LNode<int>() { data = 4, next = null };
            LNode<int> l03 = new LNode<int>() { data = 3, next = null };
            LNode<int> l02 = new LNode<int>() { data = 2, next = null };
            LNode<int> l01 = new LNode<int>() { data = 1, next = null };
            LNode<int> head = new LNode<int>() { data = 4399, next = null }; //头节点不存储数据,标记入口位置
            head.next = l01;
            l01.next = l02;
            l02.next = l03;
            l03.next = l04;
            l04.next = l05;

            
            Console.WriteLine($"order{head.next.data},{head.next.next.data},{head.next.next.next.data}," +
               $"{head.next.next.next.next.data},{head.next.next.next.next.next.data}");

            LNode<int>.Reserve03(head);
           

            Console.WriteLine($"order{head.next.data},{head.next.next.data},{head.next.next.next.data}," +
                $"{head.next.next.next.next.data},{head.next.next.next.next.next.data}");

            Console.ReadKey();
        }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值